我可以将ndarray存储在这样的文本文件中:
>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> np.savetxt('a.out',a,'%d')
然后文件a.out看起来像这样:
1 2 3
4 5 6
现在,我可以加载它:
>>> b = np.loadtxt('a.out','int')
>>> b
array([[1, 2, 3],
[4, 5, 6]])
到目前为止(2,3)阵列非常好。
现在我想对(1,2,3)数组做同样的事情。
>>> a = np.array([[[1,2,3],[4,5,6]],[[3,2,1],[6,5,4]]])
>>> a
array([[[1, 2, 3],
[4, 5, 6]],
[[3, 2, 1],
[6, 5, 4]]])
>>> np.savetxt('a.out',a,'%d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ckim/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 1160, in savetxt
% (str(X.dtype), format))
TypeError: Mismatch between array dtype ('int64') and format specifier ('%d %d')
存储阵列的正确方法是什么?以及如何加载它?我阅读了该文档,但没有很好地解释如何为复杂的ndarray指定格式。
我试过了(不是&#39;%s&#39;,&#39;%sf&#39;,int32,int4,..)
np.savetxt('a.out',a,'%s')
它给了我一个档案
[1 2 3] [4 5 6]
[3 2 1] [6 5 4]
更像是它,但我不知道如何加载它。我在下面尝试无济于事。
>>> b = np.loadtxt('a.out','%d')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ckim/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 873, in loadtxt
dtype = np.dtype(dtype)
TypeError: data type "%d" not understood
>>> b = np.loadtxt('a.out','%s')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ckim/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 873, in loadtxt
dtype = np.dtype(dtype)
TypeError: data type "%s" not understood
请有人帮帮我。谢谢!