0

Given the following array:

    complete_matrix = numpy.array([
    [0, 1, 2, 4],
    [1, 0, 3, 5],
    [2, 3, 0, 6]])

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.

Dana Gray
  • 1,605
  • 2
  • 13
  • 8

2 Answers2

2

Is this question different from the one here: Finding the row with the highest average in a numpy array

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))

You can always create a weight matrix where the weight is 0 for diagonal entries, and 1 otherwise.

Community
  • 1
  • 1
John Doe
  • 423
  • 3
  • 10
1

Something like:

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]
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Jon Clements
  • 138,671
  • 33
  • 247
  • 280