I can store ndarray in a text file like this :
>>> 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')
then file a.out looks like this :
1 2 3
4 5 6
now, I can load it back :
>>> b = np.loadtxt('a.out','int')
>>> b
array([[1, 2, 3],
[4, 5, 6]])
so far so good for (2, 3) array.
Now I want to do the same for (1, 2, 3) array.
>>> 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')
What is the correct way to store the array? and how can I load it back? I read the document but it is not well explaining about how to specify the format for a complex ndarray.
I tried (not document for '%s', '%sf', int32, int4, ..)
np.savetxt('a.out',a,'%s')
and it gives me a.out file
[1 2 3] [4 5 6]
[3 2 1] [6 5 4]
that's more like it, but I don't know how to load it. I tried below to no avail.
>>> 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
Please anybody help me with this. Thanks!