在python中访问稀疏矩阵中的行

2024-04-24 08:45:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我把稀疏矩阵存储在一个可变的稀疏矩阵中

sparse_mat = sparse.coo_matrix((freq,(data_obs,data_feature)))

sparse_mat
<540x5550 sparse matrix of type '<type 'numpy.string_'>'
with 9068 stored elements in COOrdinate format>

现在我想访问这个稀疏矩阵的第一行

我正在努力

 sparse_mat[1:,]

但它给出的错误是

TypeError: 'coo_matrix' object has no attribute '__getitem__'

这怎么可能呢。谢谢


Tags: ofnumpydatastringtypewith矩阵matrix
2条回答

您应该使用scipy.sparse来存储和操作稀疏矩阵,而不是推出一个自己开发的矩阵。你知道吗

您可以使用sparse_mat.getrow(1)(参见http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html)。它返回一个稀疏矩阵,如果需要,可以使用.todense()将其转换为密集矩阵

相关问题 更多 >