【发布时间】:2012-01-16 21:20:30
【问题描述】:
我最初问了一个相关问题here,但似乎并没有真正得到任何结果。也许如果我更具体地改写其中的一部分,它可能会有所帮助....
我使用 Matlab 的稀疏格式(HDF5、csc I 相信),我正在尝试使用 Pytables 直接对它们进行操作, 但还没有成功。使用 h5py 我可以做到以下几点:
# Method 1: uses h5py (WORKS)
f1 = h5py.File(fname)
data = f1['M']['data']
ir = f1['M']['ir']
jc = f1['M']['jc']
M = scipy.sparse.csc_matrix((data, ir, jc))
但如果我尝试在 Pytables 中做同样的事情:
# Method 2: uses pyTables (DOESN'T WORK)
f2 = tables.openFile(fname)
data = f2.root.M.data
ir = f2.root.M.ir
jc = f2.root.M.jc
M = scipy.sparse.csc_matrix( (data,ir,jc) )
这失败(经过长时间的等待)并出现错误:
TypeError Traceback (most recent call last)
/home/tdiethe/BMJ/<ipython console> in <module>()
/usr/lib/python2.6/dist-packages/scipy/sparse/compressed.pyc in __init__(self, arg1, shape, dtype, copy, dims, nzmax)
56 self.indices = np.array(indices, copy=copy)
57 self.indptr = np.array(indptr, copy=copy)
---> 58 self.data = np.array(data, copy=copy, dtype=getdtype(dtype, data))
59 else:
60 raise ValueError, "unrecognized %s_matrix constructor usage" %\
/usr/lib/python2.6/dist-packages/scipy/sparse/sputils.pyc in getdtype(dtype, a, default)
69 canCast = False
70 else:
---> 71 raise TypeError, "could not interpret data type"
72 else:
73 newdtype = np.dtype(dtype)
TypeError: could not interpret data type
看着f2:
In [63]: f2.root.M.data
Out[63]:
/M/data (CArray(4753606,), zlib(3)) ''
atom := Float64Atom(shape=(), dflt=0.0)
maindim := 0
flavor := 'numpy'
byteorder := 'little'
chunkshape := (8181,)
In [64]: f2.root.M.ir
Out[64]:
/M/ir (CArray(4753606,), zlib(3)) ''
atom := UInt64Atom(shape=(), dflt=0)
maindim := 0
flavor := 'numpy'
byteorder := 'little'
chunkshape := (8181,)
In [65]: f2.root.M.jc
Out[65]:
/M/jc (CArray(133339,), zlib(3)) ''
atom := UInt64Atom(shape=(), dflt=0)
maindim := 0
flavor := 'numpy'
byteorder := 'little'
chunkshape := (7843,)
我有两个问题:
- 如何使用 pytables 加载此文件
- 我是否需要转换为 scipy 稀疏矩阵才能对其执行操作,或者我可以直接对磁盘文件执行操作(矩阵乘法等...) - 即无需加载文件进入内存(如果不是,那么使用 pytables 有什么意义?)?
【问题讨论】:
-
这些错误来自 scipy。你能检查一下说只是 numpy 对
data、ir或jc进行操作的能力吗? numpy 对数据有什么看法(即 dtype、shape 等)?结果是你所期望的吗?它们是否与scipy.sparse.csc_matrix中对该调用签名的预期相符? -
啊,是的,看来我所要做的就是:
M = sparse.csc_matrix( (f2.root.M.data[...], f2.root.M.ir[...], f2.root.M.jc[...]) )还不确定第二个问题吗? PyTables 上似乎只有元素操作可用?
标签: python matlab numpy scipy sparse-matrix