In [172]: from scipy import sparse
In [173]: M=sparse.csr_matrix(np.eye(10))
In [174]: np.save('test.npy',M)
In [175]: f=np.load('test.npy')
In [176]: f
Out[176]:
array(<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 10 stored elements in Compressed Sparse Row format>, dtype=object)
In [177]: f.item()
Out[177]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 10 stored elements in Compressed Sparse Row format>
In [178]: f.shape
Out[178]: ()
直接使用泡菜:
In [181]: with open('test.pkl','wb') as f:
...: pickle.dump(M,f)
In [182]: with open('test.pkl','rb') as f:
...: M1=pickle.load(f)
In [183]: M1
Out[183]:
<10x10 sparse matrix of type '<class 'numpy.float64'>'
with 10 stored elements in Compressed Sparse Row format>
f = np.load('tr_tfidf.npy')
f ## returns the below.
array(<404288x83766 sparse matrix of type '<class 'numpy.float64'>'
with 2117757 stored elements in Compressed Sparse Row format>, dtype=object)
2条答案
按热度按时间kiayqfof1#
注意
dtype=object
Package 器。其形状为()
,0d。稀疏矩阵不是常规数组或子类。因此np.save
将其 Package 在一个对象数组中,并让对象自己的pickle
方法负责写入。直接使用泡菜:
最新的
scipy
版本具有保存稀疏矩阵的新功能https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.save_npz.html
dz6r00yl2#
我自己解决的。
我相信XYZ.shape也可以使用引用。