Given a matrix of type `scipy.sparse.coo_matrix` h

2019-05-07 05:51发布

问题:

Given a sparse matrixR of type scipy.sparse.coo_matrix of shape 1.000.000 x 70.000 I figured out that

row_maximum = max(R.getrow(i).data)

will give me the maximum value of the i-th row.

What I need now is the index corresponding to the value row_maximum.

Any ideas how to achieve that?

Thanks for any advice in advance!

回答1:

getrow(i) returns a 1 x n CSR matrix, which has an indices attribute that gives the row indices of the corresponding values in the data attribute. (We know the shape is 1 x n, so we don't have to deal with the indptr attribute.) So this will work:

row = R.getrow(i)
max_index = row.indices[row.data.argmax()] if row.nnz else 0

We have to deal with the case where row.nnz is 0 separately, because row.data.argmax() will raise an exception if row.data is an empty array.



回答2:

use numpy.argmax (or scipy.argmax which is the same thing)

index_of_maximum = scipy.argmax(R.getrow(i).data)