考虑以下数组:如何识别numpy数组中平均值最大的列?
complete_matrix = numpy.array([
[0, 1, 2, 4],
[1, 0, 3, 5],
[2, 3, 0, 6]])
我想与最高平均识别列,不包括对角线零。因此,在这种情况下,我可以将complete_matrix [:,3]识别为平均值最高的列。
考虑以下数组:如何识别numpy数组中平均值最大的列?
complete_matrix = numpy.array([
[0, 1, 2, 4],
[1, 0, 3, 5],
[2, 3, 0, 6]])
我想与最高平均识别列,不包括对角线零。因此,在这种情况下,我可以将complete_matrix [:,3]识别为平均值最高的列。
这个问题是不是从一个不同的位置:Finding the row with the highest average in a numpy array
据我了解,唯一的区别就是在这个岗位矩阵是不是一个方阵。如果这是故意的,你可以尝试使用重量。因为我不完全理解你的意图,下面的解决方案将0重为零的条目,否则为1:
numpy.argmax(numpy.average(complete_matrix,axis=0, weights=complete_matrix!=0))
你总是可以创建一个权重矩阵,其中权重为0的对角元素,否则为1。
喜欢的东西:
import numpy
complete_matrix = numpy.array([
[0, 1, 2, 4],
[1, 0, 3, 5],
[2, 3, 0, 6]])
print complete_matrix[:,numpy.argmax(numpy.mean(complete_matrix, 0))]
# [4 5 6]
我[添加了此答案](http://stackoverflow.com/a/17420604/832621)考虑到排除零的对角线 –