本文介绍了给定类型为scipy.sparse.coo_matrix的矩阵,如何确定每行最大值的索引和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出形状为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的矩阵,如何确定每行最大值的索引和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 13:00