在 HDF5 (PyTables) 中存储 numpy 稀疏矩阵 [英] Storing numpy sparse matrix in HDF5 (PyTables)

查看:50
本文介绍了在 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()

有什么想法吗?

谢谢

推荐答案

CSR 矩阵可以从它的 dataindicesindptr属性.这些只是常规的 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 稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆