2013-08-27 118 views
2

保存如果我保存使用numpy.save()企业社会责任矩阵可用csr_matrix,然后尝试通过numpy.load加载它在(),一大量属性消失:特别是没有形状,并且不可能通过索引访问值。这是正常的吗?为什么np.load不会返回由np.save

在下面的例子中,我从三个数组创建一个CSR矩阵:数据,索引和索引指针。然后保存它,将其加载回来,并演示保存版本中形状和索引操作的失败。

> import numpy as np 
> import scipy as sp 
> import scipy.sparse as ssp 

> wd 
Out[1]: 
array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=int16) 

> wi 
Out[1]: 
array([200003,  1, 200009, 300000, 200002, 200006, 200007, 250000, 
     300500, 200010, 300501, 200001, 200000,  0, 200008, 200004, 
     200005, 200011, 200018,  2, 200019, 200013, 300001, 200014, 
     200015, 200022, 200012, 200020, 200021, 200016, 200017, 200023, 
     200027,  2, 200030, 200032, 200028, 200033, 200031, 200029, 
     200026, 200025, 200024, 200047,  2, 200042, 200045, 200046, 
     200028, 200038, 200040, 200039, 200036, 200037, 200012, 200048, 
     200041, 200035, 200044, 200043, 200034, 200049,  3, 200050, 
      4], dtype=int32) 

> wp 
Out[1]: array([ 0, 18, 31, 43, 61, 65], dtype=int32) 

> ww = ssp.csr_matrix((wd,wi,wp)) 

> ww.shape 
Out[1]: (5, 300502) 

> ww[2,3] 
Out[1]: 0 

> ww[0,0] 
Out[1]: 1 

> np.save('/Users/bryanfeeney/Desktop/ww.npy', ww) 
> www = np.load('/Users/bryanfeeney/Desktop/ww.npy') 

> www.shape 
Out[1]:() 

> www[2,3] 
Traceback (most recent call last): 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/interactiveshell.py", line 2732, in run_code 
    exec(code_obj, self.user_global_ns, self.user_ns) 
    File "<ipython-input-1-35f1349fb755>", line 1, in <module> 
    www[2,3] 
IndexError: 0-d arrays can only use a single() or a list of newaxes (and a single ...) as an index 

> www[0,0] 
Traceback (most recent call last): 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/core/interactiveshell.py", line 2732, in run_code 
    exec(code_obj, self.user_global_ns, self.user_ns) 
    File "<ipython-input-1-43c5da404060>", line 1, in <module> 
    www[0,0] 
IndexError: 0-d arrays can only use a single() or a list of newaxes (and a single ...) as an index 

以下是python运行时,numpy和scipy的版本信息。

> sys.version 
Out[1]: '3.3.2 (default, May 21 2013, 11:50:47) \n[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))]' 

> np.__version__ 
Out[1]: '1.7.1' 

> sp.__version__ 
Out[1]: '0.12.0' 
+0

针对该文档的评论状态np.load/store在阵列上工作,可能就是这样,但是没有任何干净的方式存储和加载CSR矩阵吗?到目前为止,我一直在使用存储和加载数据,索引和indptr的技巧,这似乎凌乱 – Feenaboccles

+0

跨越评论和帖子。我只能看到“混乱”的方式。但其他人可能会回答 – doctorlove

+0

[可移植数据格式保存/加载scipy稀疏csr \ _matrix]可能的重复(http://stackoverflow.com/questions/8955448/save-load-scipy-sparse-csr-matrix-in-便携式数据格式) –

回答

0

三个变量wdwiwp弥补你的稀疏矩阵。你需要保存所有这三个,因为numpy save涉及numpy数组。
然后有他们装,说是WWD,第一次世界大战和WWP作出新的矩阵

new_csr = csr_matrix((wwd, wwi, wwp), shape=(M, N)) 

了类似的讨论参见here

0

这似乎是一个错误,但你可以泡制整个稀疏矩阵对象:

import pickle 
with open('ww.pkl', 'w') as f: 
    pickle.dump(w, f) 

,当你想加载:

with open('ww.pkl') as f: 
    ww = pickle.load(f) 
+0

由于约定是对numpy格式使用'npy'扩展名,'pickle.dump'保存的文件不是*使用numpy文件格式,所以我建议在'pickle'时使用不同的文件扩展名使用(例如'pkl')。 –

+0

@WarrenWeckesser很好的观察,我已经编辑了答案 –

相关问题