加载 .npy 文件加载一个空数组

Loading .npy File Loads an Empty Array

我有一个大小为

的 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.
()

提前致谢。

哈哈.. 我刚刚..

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 也适用于参考文献。

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 包装器。这具有形状 (), 0d。稀疏矩阵不是常规数组或子类。因此 np.save 求助于将其包装在对象数组中,并让对象自己的 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]: ()

直接使用 pickle:

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