如何识别numpy数组中具有最大平均值的列? [英] How can I identify the column with the greatest average in a numpy array?
问题描述
给出以下数组:
complete_matrix = numpy.array([
[0, 1, 2, 4],
[1, 0, 3, 5],
[2, 3, 0, 6]])
我想确定平均值最高的列,不包括对角线零。因此,在这种情况下,我将能够确定complete_matrix [:,3]为平均值最高的列。
I would like to identify the column with the highest average, excluding the diagonal zeros. So, in this case, I would be able to identify complete_matrix[:,3] as being the column with the highest average.
推荐答案
这个问题与这里的问题是否有所不同:在numpy数组中查找具有最高平均值的行
Is this question different from the one here: Finding the row with the highest average in a numpy array
据我了解,唯一的区别是这篇文章中的矩阵不是方阵。如果这是故意的,则可以尝试使用权重。由于我无法完全理解您的意图,因此以下解决方案将0权重分配给零项,否则分配1权重:
As far as I understand, the only difference is the matrix in this post isn't a square matrix. In case this was deliberate, you could try using weights. Since I do not understand your intent fully, the following solution assigns 0 weight to zero entries, 1 otherwise:
numpy.argmax(numpy.average(complete_matrix,axis=0, weights=complete_matrix!=0))
您可以随时创建一个权重矩阵,其中对角线条目的权重为0,否则为1。
You can always create a weight matrix where the weight is 0 for diagonal entries, and 1 otherwise.
这篇关于如何识别numpy数组中具有最大平均值的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!