Np.savetxt Triggers Valueerror. Why?
Earlier in the program, I calculate the matrices P and K. I need the eigenvalues of PKP. Since this takes a while (it's a 10000 x 10000 matrix), I want to save the result someplace
Solution 1:
np.linalg.eig
outputs two np.ndarray
one of shape (1000,), the second of shape (1000, 1000).
You should either save them into different files or use np.savez
or np.savez_compressed
istead:
np.savez('eigdata', *eigs)
And restore them later:
w, v = np.load('eigdata.npz').values()
Solution 2:
Using a simple example from eig
:
In [332]: a = np.array([[1, 1j], [-1j, 1]])
...: w,v = np.linalg.eig(a)
In [334]: np.savez('eig.npz', w=w, v=v)
In [335]: d = np.load('eig.npz')
In [336]: list(d.keys())
Out[336]: ['w', 'v']
In [337]: d['w']
Out[337]: array([2.+0.j, 0.+0.j])
In [338]: d['v']
Out[338]:
array([[ 0. +0.70710678j, 0.70710678+0.j ],
[ 0.70710678+0.j , -0. +0.70710678j]])
Or without the keyword names:
In [339]: np.savez('eig.npz', w,v)
In [340]: d = np.load('eig.npz')
In [341]: list(d.keys())
Out[341]: ['arr_0', 'arr_1']
In [342]: d['arr_0']
Out[342]: array([2.+0.j, 0.+0.j])
i suspect that if you get a pickle error, it's because you are saving an object like a tuple or dictionary:
In [345]: np.savez('eig.npz', {'w':w, 'v':v})
In [346]: d = np.load('eig.npz')
In [347]: list(d.keys())
Out[347]: ['arr_0']
In [348]: d['arr_0']
...
ValueError: Object arrays cannot be loaded when allow_pickle=False
Recent numpy versions have become picky about this pickling parameter.
Or you could save the arrays to two files:
In [370]: np.save('eig_w.npy',w); np.save('eig_v.npy',v)
In [371]: np.load('eig_w.npy')
Out[371]: array([2.+0.j, 0.+0.j])
In [372]: np.load('eig_v.npy')
Out[372]:
array([[ 0. +0.70710678j, 0.70710678+0.j ],
[ 0.70710678+0.j , -0. +0.70710678j]])
===
With:
In [373]: eigs = np.linalg.eig(a)
In [375]: np.savez('eig.npz', eigs)
ValueError: could not broadcast input array from shape (2,2) into shape (2)
That error is produced by a np.array(eigs)
step. np.save
is intended to saving arrays. Given a tuple it tries to turn it into an array.
Saving *eigs
is like the In [339]
example above:
In [385]: np.savez('eig.npz', *eigs)
In [386]: d = np.load('eig.npz')
In [387]: list(d.keys())
Out[387]: ['arr_0', 'arr_1']
In [388]: d['arr_1']
Out[388]:
array([[ 0. +0.70710678j, 0.70710678+0.j ],
[ 0.70710678+0.j , -0. +0.70710678j]])
In [389]: d['arr_0']
Out[389]: array([2.+0.j, 0.+0.j])
Post a Comment for "Np.savetxt Triggers Valueerror. Why?"