简体   繁体   English

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

[英]accessing rows in a sparse matriox in python

I have stored sparse matrix in a variable sparse_mat 我已经将稀疏矩阵存储在变量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>

Now I want to access the first row of this sparse matrix 现在我要访问此稀疏矩阵的第一行

I am trying 我在尝试

 sparse_mat[1:,]

But the error it gives is 但是它给出的错误是

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

How could this be done. 这怎么做。 thanks 谢谢

您应该使用scipy.sparse来存储和处理稀疏矩阵,而不要推出一个自制的矩阵。

You can use sparse_mat.getrow(1) (see http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html ). 您可以使用sparse_mat.getrow(1) (请参阅http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html )。 It returns a sparse matrix, which you can convert to a dense one if necessary with .todense() 它返回一个稀疏矩阵,您可以在必要时转换成一个密集与.todense()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM