numpy 加载.npy文件加载空数组

dohp0rv5  于 2023-05-07  发布在  其他
关注(0)|答案(2)|浏览(218)

我有一个TfIDF矩阵

tr_tfidf_q1.shape, tr_tfidf_q2.shape which gives 
( (404288, 83766), (404288, 83766) )

现在我保存它使用

np.save('tr_tfidf_q1.npy', tr_tfidf_q1)

当我像这样加载文件时

f = np.load('tr_tfidf_q1.npy') 
f.shape() ## returns an empty array.
()

先谢谢你了。

kiayqfof

kiayqfof1#

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)

注意dtype=object Package 器。其形状为(),0d。稀疏矩阵不是常规数组或子类。因此np.save将其 Package 在一个对象数组中,并让对象自己的pickle方法负责写入。

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>

最新的scipy版本具有保存稀疏矩阵的新功能
https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.save_npz.html

dz6r00yl

dz6r00yl2#

我自己解决的。

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)

我相信XYZ.shape也可以使用引用。

相关问题