我如何在python中检索julia在.jld文件中存储的稀疏矩阵?

时间:2015-11-19 18:39:40

标签: python python-2.7 julia hdf5

使用julia,我可以将稀疏矩阵保存在.jld文件中(使用HDF5格式),如下所示:

env

现在我想在python中检索这个矩阵(使用h5py),所以我尝试了以下内容:

a=spzeros(3,3);
a[1,1]=2.0
a[2,1]=1.0
a[3,1]=5
@save("sparsematrix.jld",a)

打印import h5py filename="sparsematrix.jld" f = h5py.File(filename, 'r') data= f["a"][()] f.close() 将返回data,因此我尝试使用以下内容访问对象引用:(3, 3, <HDF5 object reference>, <HDF5 object reference>, <HDF5 object reference>),返回f[data[2]]但现在我卡住了。

那么如何从.jld文件中获取稀疏矩阵呢?

1 个答案:

答案 0 :(得分:1)

好的,在围绕CSC格式包围后,我自己找到了它:

import h5py 
from scipy.sparse import csc_matrix

filename="sparsematrix.jld"
f = h5py.File(filename, 'r')
data= f["a"][()]

column_ptr=f[data[2]][:]-1 ## correct indexing from julia (starts at 1)
indices=f[data[3]][:]-1 ## correct indexing
values =f[data[4]][:]
csc_matrix((values,indices,column_ptr), shape=(data[0],data[1])).toarray()

f.close()