中存储Numpy稀疏矩阵

中存储Numpy稀疏矩阵

本文介绍了在HDF5(PyTables)中存储Numpy稀疏矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在PyTables中存储numpy csr_matrix.我收到此错误:

I am having trouble storing a numpy csr_matrix with PyTables. I'm getting this error:

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

有什么想法吗?

谢谢

推荐答案

可以从其dataindicesindptr属性完全重建CSR矩阵.这些只是常规的numpy数组,因此将它们作为3个单独的数组存储在pytables中,然后将它们传递回csr_matrix的构造函数应该没有问题.请参见 scipy文档.

A CSR matrix can be fully reconstructed from its data, indices and indptr attributes. These are just regular numpy arrays, so there should be no problem storing them as 3 separate arrays in pytables, then passing them back to the constructor of csr_matrix. See the scipy docs.

Pietro的答案指出,shape成员也应存储

Pietro's answer has pointed out that the shape member should also be stored

这篇关于在HDF5(PyTables)中存储Numpy稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 11:44