Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the row with the highest average in a numpy array

Given the following array:

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

I would like to identify the row with the highest average, excluding the diagonal zeros. So, in this case, I would be able to identify complete_matrix[:,3] as being the row with the highest average.

like image 311
Dana Gray Avatar asked Jun 30 '13 22:06

Dana Gray


3 Answers

Note that the presence of the zeros doesn't affect which row has the highest mean because all rows have the same number of elements. Therefore, we just take the mean of each row, and then ask for the index of the largest element.

#Take the mean along the 1st index, ie collapse into a Nx1 array of means
means = np.mean(complete_matrix, 1)
#Now just get the index of the largest mean
idx = np.argmax(means)

idx is now the index of the row with the highest mean!

like image 116
kjoppy Avatar answered Oct 17 '22 16:10

kjoppy


You don't need to worry about the 0s, they shouldn't effect how the averages compare since there will presumably be one in each row. Hence, you can do something like this to get the index of the row with the highest average:

>>> import numpy as np 
>>> complete_matrix = np.array([
...     [0, 1, 2, 4],
...     [1, 0, 3, 5],
...     [2, 3, 0, 6],
...     [4, 5, 6, 0]])
>>> np.argmax(np.mean(complete_matrix, axis=1))
3

Reference:

  • numpy.mean
  • numpy.argmax
like image 5
arshajii Avatar answered Oct 17 '22 15:10

arshajii


As pointed out by a lot of people, presence of zeros isn't an issue as long as you have the same number of zeros in each column. Just in case your intention was to ignore all the zeros, preventing them from participating in the average computation, you could use weights to suppress the contribution of the zeros. 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.

like image 4
John Doe Avatar answered Oct 17 '22 16:10

John Doe