问题描述
给出形状为1.000.000 x 70.000
的类型为scipy.sparse.coo_matrix
的稀疏矩阵R
,我发现了
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)
将给我第i行的最大值.
will give me the maximum value of the i-th row.
我现在需要的是与值row_maximum
对应的索引.
What I need now is the index corresponding to the value row_maximum
.
有什么想法可以实现这一目标吗?
Any ideas how to achieve that?
谢谢您的任何建议!
推荐答案
getrow(i)
返回1 x n CSR矩阵,该矩阵具有indices
属性,该属性给出data
属性中相应值的行索引. (我们知道形状是1 x n,因此我们不必处理indptr
属性.)因此这将起作用:
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
我们必须分别处理row.nnz
为0的情况,因为如果row.data
为空数组,则row.data.argmax()
将引发异常.
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.
这篇关于给定类型为scipy.sparse.coo_matrix的矩阵,如何确定每行最大值的索引和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!