Get the Position of the Largest Value in a Multi-Dimensional Numpy Array

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)

Get the position of the largest value in a multi-dimensional NumPy array

The argmax() method should help.

Update

(After reading comment) I believe the argmax() method would work for multi dimensional arrays as well. The linked documentation gives an example of this:

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

Update 2

(Thanks to KennyTM's comment) You can use unravel_index(a.argmax(), a.shape) to get the index as a tuple:

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

how to get the index of the largest n values in a multi-dimensional numpy array

I don't have access to bottleneck, so in this example I am using argsort, but you should be able to use it in the same way:

#!/usr/bin/env python
import numpy as np
N = 4
a = np.random.random(20).reshape(4, 5)
print(a)

# Convert it into a 1D array
a_1d = a.flatten()

# Find the indices in the 1D array
idx_1d = a_1d.argsort()[-N:]

# convert the idx_1d back into indices arrays for each dimension
x_idx, y_idx = np.unravel_index(idx_1d, a.shape)

# Check that we got the largest values.
for x, y, in zip(x_idx, y_idx):
print(a[x][y])

How to get indexes of k maximum values from a numpy multidimensional array

Couple of approaches with np.argpartition and np.argsort for ndarrays -

def k_largest_index_argpartition_v1(a, k):
idx = np.argpartition(-a.ravel(),k)[:k]
return np.column_stack(np.unravel_index(idx, a.shape))

def k_largest_index_argpartition_v2(a, k):
idx = np.argpartition(a.ravel(),a.size-k)[-k:]
return np.column_stack(np.unravel_index(idx, a.shape))

def k_largest_index_argsort(a, k):
idx = np.argsort(a.ravel())[:-k-1:-1]
return np.column_stack(np.unravel_index(idx, a.shape))

Discussion on two versions with argpartition

Difference between k_largest_index_argpartition_v1 and k_largest_index_argpartition_v2 is how we are using argparition. In the first version, we are negating input array and then using argpartition to get the indices for the smallest k indices, thus effectively getting the largest k indices, whereas in the second version, we are getting the first a.size-k smallest indices and then we are choosing the leftover largest k indices.

Also, its worth mentioning here that with argpartition, we are not getting the indices in their sorted order. If the sorted order is needed, we need to feed in range array to np.argpartition, as mentioned in this post.

Sample runs -

1) 2D case :

In [42]: a    # 2D array
Out[42]:
array([[38, 14, 81, 50],
[17, 65, 60, 24],
[64, 73, 25, 95]])

In [43]: k_largest_index_argsort(a, k=2)
Out[43]:
array([[2, 3],
[0, 2]])

In [44]: k_largest_index_argsort(a, k=4)
Out[44]:
array([[2, 3],
[0, 2],
[2, 1],
[1, 1]])

In [66]: k_largest_index_argpartition_v1(a, k=4)
Out[66]:
array([[2, 1], # Notice the order is different
[2, 3],
[0, 2],
[1, 1]])

2) 3D case :

In [46]: a # 3D array
Out[46]:
array([[[20, 98, 27, 73],
[33, 78, 48, 59],
[28, 91, 64, 70]],

[[47, 34, 51, 19],
[73, 38, 63, 94],
[95, 25, 93, 64]]])

In [47]: k_largest_index_argsort(a, k=2)
Out[47]:
array([[0, 0, 1],
[1, 2, 0]])

Runtime test -

In [56]: a = np.random.randint(0,99999999999999,(3000,4000))

In [57]: %timeit k_largest_index_argsort(a, k=10)
1 loops, best of 3: 2.18 s per loop

In [58]: %timeit k_largest_index_argpartition_v1(a, k=10)
10 loops, best of 3: 178 ms per loop

In [59]: %timeit k_largest_index_argpartition_v2(a, k=10)
10 loops, best of 3: 128 ms per loop

How to return the highest value from a multi dimensional array?

Since you mentioned in a comment that you are using numpy ...

>>> import numpy as np
>>> a = np.random.rand(3,3)
>>> a
array([[ 0.43852835, 0.07928864, 0.33829191],
[ 0.60776121, 0.02688291, 0.67274362],
[ 0.2188034 , 0.58202254, 0.44704166]])
>>> a.max(axis=1)
array([ 0.43852835, 0.67274362, 0.58202254])

edit: the documentation is here

Finding the Max value in a two dimensional Array

Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):

>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]

>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers)) # max numbers from each sublist
[1, 2, 2, 3, 4]

>>> max(map(max, numbers)) # max of those max-numbers
4

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

>>> a.argmax(axis=0)

array([1, 1, 0])

Python: How to get values of a multi-dimensional array at certain indexed positions?

As in the one-dimensional answer you linked, you can do this elegantly in 2 dimensions with numpy:

a = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
rows, columns = zip([0, 0], [1, 1], [2, 2])
print(a[rows, columns])

The output of the print will be:

array([0, 4, 8])


Related Topics



Leave a reply



Submit