I'm trying to save a 4-D array using numpy.savetxt, and it does not appear to work.
我尝试使用numpy保存一个4-D数组。它看起来并不起作用。
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
我编辑npyio。py和打印出实际的类型错误,而不是重新生成的类型错误。
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".
numpy文档没有提到数组维度,只是说它是“arrayoid”。
http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html
http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html
numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ')[source]
numpy。savetxt(帧,X,fmt = ' %。18 e,分隔符= ' ',换行符= ' \ n ',头=”,页脚= "评论= ' # ')[源]
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.
fname:文件名或文件句柄,如果文件名以.gz结尾,则该文件将以压缩gzip格式自动保存。loadtxt可以透明地理解压缩文件。
X : array_like Data to be saved to a text file.
X: array_like数据保存到文本文件中。
Is anybody else seeing this? Is it expected behaviour?
还有其他人看到这个吗?这是预期的行为吗?
1 个解决方案
#1
1
Look again where you changed the error message:
再次查看您更改错误消息的位置:
for row in X:
try:
fh.write(asbytes(format % tuple(row) + newline))
except TypeError:
...
Try this with your mat
:
用你的垫子试试这个:
print('%.18e %.18e %.18e'%tuple(np.array([1,2,3]))) # working
for row in mat:
print('%.18e %.18e %.18e'%tuple(row)) # your error
This is straight forward Python string formatting operation. It only works when the number of elements in row (after conversion to a tuple) matches the number of %
specifiers in the format. And the elements have to match - in this case numbers that can be displayed with the %e
.
这是直接的Python字符串格式化操作。只有当行中的元素数量(转换为tuple)与格式中%说明符的数量匹配时才有效。元素必须匹配——在这个情况下,可以用%e显示的数字。
There's no provision in savetxt
to iterate over the higher dimensions of your array. You have to do that kind of iteration yourself.
在savetxt中没有提供对数组的更高维度进行迭代的规定。你必须自己做这种迭代。
Roughly:
约:
f = open('txt.txt', 'w')
for block in Mat:
for subblock in block:
np.savetxt(f, block, fmt=...) # write to open file
f.write('\n') # spacer line
f.write('\n') # another spacer
f.close()
A more detailed answer along the same line:
更详细的答案在同一条线上:
How to write a multidimensional array to a text file?
如何将多维数组写入文本文件?
#1
1
Look again where you changed the error message:
再次查看您更改错误消息的位置:
for row in X:
try:
fh.write(asbytes(format % tuple(row) + newline))
except TypeError:
...
Try this with your mat
:
用你的垫子试试这个:
print('%.18e %.18e %.18e'%tuple(np.array([1,2,3]))) # working
for row in mat:
print('%.18e %.18e %.18e'%tuple(row)) # your error
This is straight forward Python string formatting operation. It only works when the number of elements in row (after conversion to a tuple) matches the number of %
specifiers in the format. And the elements have to match - in this case numbers that can be displayed with the %e
.
这是直接的Python字符串格式化操作。只有当行中的元素数量(转换为tuple)与格式中%说明符的数量匹配时才有效。元素必须匹配——在这个情况下,可以用%e显示的数字。
There's no provision in savetxt
to iterate over the higher dimensions of your array. You have to do that kind of iteration yourself.
在savetxt中没有提供对数组的更高维度进行迭代的规定。你必须自己做这种迭代。
Roughly:
约:
f = open('txt.txt', 'w')
for block in Mat:
for subblock in block:
np.savetxt(f, block, fmt=...) # write to open file
f.write('\n') # spacer line
f.write('\n') # another spacer
f.close()
A more detailed answer along the same line:
更详细的答案在同一条线上:
How to write a multidimensional array to a text file?
如何将多维数组写入文本文件?