I'm trying to save a 4-D array using numpy.savetxt, and it does not appear to work.
In [13]: mat = np.zeros((3,3,2,2))
In [14]: mat[0][0][0][0] = 1.5e+10
In [15]: mat[0][0][0][1] = 1.6e+10
In [16]: mat[0][0][1][0] = 1.7e+10
In [17]: mat[0][0][1][1] = 1.8e+10
In [18]: np.savetxt("/tmp/save_mat", mat)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
----> 1 np.savetxt("/tmp/save_mat", mat)
python2.7/site-packages/numpy/lib/npyio.pyc in savetxt(fname, X, fmt, delimiter, newline, header, footer, comments)
1158 print(e)
1159 raise TypeError("Mismatch between array dtype ('%s') and "
-> 1160 "format specifier ('%s')"
1161 % (str(X.dtype), format))
1162 if len(footer) > 0:
TypeError: Mismatch between array dtype ('float64') and format specifier ('%.18e %.18e %.18e')
I edited npyio.py and printed out the actual TypeError instead of the re-raised TypeError, and it was
float argument required, not numpy.ndarray
It works fine if I use the binary save method
In [20]: fd = open("/tmp/save_mat", "w")
In [21]: np.save(fd, mat)
In [22]: fd.close()
And there is a nonzero file created
$ ls -al /tmp/save_mat
-rw-r--r-- 1 368 May 11 07:17 /tmp/save_mat
The numpy documentation does not say anything about the array dimensions, just that it is "array-like".
http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html
numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ')[source]
Save an array to a text file. Parameters:
fname : filename or file handle If the filename ends in .gz, the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.
X : array_like Data to be saved to a text file.
Is anybody else seeing this? Is it expected behaviour?