简体   繁体   中英

accessing rows in a sparse matriox in python

I have stored sparse matrix in a variable 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 ). It returns a sparse matrix, which you can convert to a dense one if necessary with .todense()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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