加载.npy文件加载空数组

时间:2017-04-17 16:49:28

标签: arrays numpy save load

我有一个大小为

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

提前致谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

洛尔.. 我刚刚做了..

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