访问python中稀疏矩阵中的行

时间:2014-01-15 06:36:59

标签: python sparse-matrix

我在变量sparse_mat

中存储了稀疏矩阵
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__'

怎么可能这样做。感谢

2 个答案:

答案 0 :(得分:1)

你应该使用scipy.sparse来存储和操作稀疏矩阵,而不是推出一个本地的矩阵。

答案 1 :(得分:0)

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

将其转换为密集矩阵