我在用PyTables存储numpy csr_矩阵时遇到问题。我得到这个错误:
TypeError: objects of type ``csr_matrix`` are not supported in this context, sorry; supported objects are: NumPy array, record or scalar; homogeneous list or tuple, integer, float, complex or string
我的代码:
f = tables.openFile(path,'w')
atom = tables.Atom.from_dtype(self.count_vector.dtype)
ds = f.createCArray(f.root, 'count', atom, self.count_vector.shape)
ds[:] = self.count_vector
f.close()
有什么想法吗?
谢谢
DaveP的答案是几乎是对的。。。但对于非常稀疏的矩阵可能会导致问题:如果最后一列或行是空的,则会删除它们。因此,为了确保一切正常,还必须存储“shape”属性。
这是我经常使用的代码:
将其应用于csc矩阵是很简单的。
我已经为Python 3.6和PyTables3.x更新了Pietro Battiston的优秀答案,因为在从2.x升级的过程中,一些PyTables函数名已经更改
CSR矩阵可以从其
data
、indices
和indptr
属性完全重构。这些只是常规的numpy数组,因此将它们存储为pytables中的3个独立数组,然后将它们传递回csr_matrix
的构造函数应该没有问题。请参阅scipy docs。编辑:Pietro的回答指出,
shape
成员也应该存储相关问题 更多 >
编程相关推荐