How to Get the Index of a Maximum Element in a Numpy Array Along One Axis

How to get the index of a maximum element in a NumPy array along one axis

>>> a.argmax(axis=0)

array([1, 1, 0])

Find index of the maximum value in a numpy array

Use built-in function for it:

prediction.argmax()

output:

9

Also, that index 0 is the row number, so the max is at row 0 and column 9.

How can I index each occurrence of a max value along a given axis of a numpy array?

Try with np.where:

np.where(Q == Q.max(axis=1)[:,None])

Output:

(array([0, 0, 1, 1, 2]), array([1, 2, 0, 2, 1]))

Not quite the output you want, but contains equivalent information.

You can also use np.argwhere which gives you the zip data:

np.argwhere(Q==Q.max(axis=1)[:,None])

Output:

array([[0, 1],
[0, 2],
[1, 0],
[1, 2],
[2, 1]])

How to get the index of maximum values along 2d in a 4d numpy array

argmax only accepts scalar axis values (some other functions allow tuples). But we can reshape B:

In [18]: B.reshape(6,2,2).argmax(axis=0)
Out[18]:
array([[5, 1],
[4, 0]])

and convert those back to 2d indices:

In [21]: np.unravel_index(_18,(2,3))
Out[21]:
(array([[1, 0],
[1, 0]]),
array([[2, 1],
[1, 0]]))

those values could be reordered in various ways, for example:

In [23]: np.transpose(_21)
Out[23]:
array([[[1, 2],
[1, 1]],

[[0, 1],
[0, 0]]])

Maximum along axis in numpy

To get (e.g. print) indices of max elements, use the following
code:

for i in range(custom_array.shape[1]):
tbl = np.take(custom_array, i, axis=1)
ind = np.unravel_index(np.argmax(tbl), tbl.shape)
print(f'{i}: {ind}')

Details:

  • for i in ... - iterate over values of X2,
  • tbl - a slice of your source array with particular value of X2,
  • np.argmax(tbl) - the index of max element in a flattened array,
  • np.unravel_index - convert to indices in the underlying array (tbl).

For your data sample the result is:

0: (1, 0)
1: (0, 2)

How do I get indices of N maximum values in a NumPy array?

Newer NumPy versions (1.8 and up) have a function called argpartition for this. To get the indices of the four largest elements, do

>>> a = np.array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])
>>> a
array([9, 4, 4, 3, 3, 9, 0, 4, 6, 0])

>>> ind = np.argpartition(a, -4)[-4:]
>>> ind
array([1, 5, 8, 0])

>>> top4 = a[ind]
>>> top4
array([4, 9, 6, 9])

Unlike argsort, this function runs in linear time in the worst case, but the returned indices are not sorted, as can be seen from the result of evaluating a[ind]. If you need that too, sort them afterwards:

>>> ind[np.argsort(a[ind])]
array([1, 8, 5, 0])

To get the top-k elements in sorted order in this way takes O(n + k log k) time.

Get N maximum values and indices along an axis in a NumPy array

I'd use argsort():

top2_ind = score_matrix.argsort()[:,::-1][:,:2]

That is, produce an array which contains the indices which would sort score_matrix:

array([[1, 2, 0],
[0, 1, 2],
[0, 1, 2]])

Then reverse the columns with ::-1, then take the first two columns with :2:

array([[0, 2],
[2, 1],
[2, 1]])

Then similar but with regular np.sort() to get the values:

top2_score = np.sort(score_matrix)[:,::-1][:,:2]

Which following the same mechanics as above, gives you:

array([[ 1. ,  0.4],
[ 0.8, 0.6],
[ 0.5, 0.3]])

How to find maximum value in whole 2D array with indices

Refer to this answer, which also elaborates how to find the max value and its (1D) index, you can use argmax()

>>> a = array([[10,50,30],[60,20,40]])
>>> maxindex = a.argmax()
>>> maxindex
3

You can then use unravel_index(a.argmax(), a.shape) to get the indices as a tuple:

>>> from numpy import unravel_index
>>> unravel_index(a.argmax(), a.shape)
(1, 0)

find indices of maximum value in matrix (python)

You want np.unravel_index. The np.argmax will return an index as if the flattened version of array is traversed. The unravel_index will give you the N-D indices.

a = np.random.randint(0, 10, (4,4))
ind = np.unravel_index(np.argmax(a, axis=None), a.shape) # returns a tuple


Related Topics



Leave a reply



Submit