Table of Contents
Finding the index of the maximum value in a NumPy array can be achieved using the numpy.argmax() function. This function returns the index of the maximum value in the array, allowing for easy access to the position of the highest value. This can be useful in various applications such as data analysis and machine learning. By using numpy.argmax(), users can efficiently locate the maximum value in the array and utilize its index for further operations. This function is a valuable tool for manipulating and analyzing NumPy arrays.
Get the Index of Max Value in NumPy Array
You can use the following methods to get the index of the max value in a NumPy array:
Method 1: Get Index of Max Value in One-Dimensional Array
x.argmax()
Method 2: Get Index of Max Value in Each Row of Multi-Dimensional Array
x.argmax(axis=1)
Method 3: Get Index of Max Value in Each Column of Multi-Dimensional Array
x.argmax(axis=0)
The following examples show how to use each method in practice.
Example 1: Get Index of Max Value in One-Dimensional Array
The following code shows how to get the index of the max value in a one-dimensional NumPy array:
import numpy as np
#create NumPy array of values
x = np.array([2, 7, 9, 4, 4, 6, 3])
#find index that contains max value
x.argmax()
2The argmax() function returns a value of 2.
This tells us that the value in index position 2 of the array contains the maximum value.
If we look at the original array, we can see that the value in index position 2 is 9, which is indeed the maximum value in the array.
Example 2: Get Index of Max Value in Each Row of Multi-Dimensional Array
The following code shows how to get the index of the max value in each row of a multi-dimensional NumPy array:
import numpy as np
#create multi-dimentsional NumPy array
x = np.array([[4, 2, 1, 5], [7, 9, 2, 0]])
#view NumPy array
print(x)
[[4 2 1 5]
[7 9 2 0]]
#find index that contains max value in each row
x.argmax(axis=1)
array([3, 1], dtype=int32)
- The max value in the first row is located in index position 3.
- The max value in the second row is located in index position 1.
Example 3: Get Index of Max Value in Each Column of Multi-Dimensional Array
The following code shows how to get the index of the max value in each column of a multi-dimensional NumPy array:
import numpy as np
#create multi-dimentsional NumPy array
x = np.array([[4, 2, 1, 5], [7, 9, 2, 0]])
#view NumPy array
print(x)
[[4 2 1 5]
[7 9 2 0]]
#find index that contains max value in each column
x.argmax(axis=0)
array([1, 1, 1, 0], dtype=int32)
From the results we can see:
- The max value in the first column is located in index position 1.
- The max value in the second column is located in index position 1.
- The max value in the third column is located in index position 1.
- The max value in the fourth column is located in index position 0.
Related:
The following tutorials explain how to perform other common operations in Python:
Cite this article
stats writer (2024). How can I find the index of the maximum value in a NumPy array?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-the-index-of-the-maximum-value-in-a-numpy-array/
stats writer. "How can I find the index of the maximum value in a NumPy array?”." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-the-index-of-the-maximum-value-in-a-numpy-array/.
stats writer. "How can I find the index of the maximum value in a NumPy array?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-the-index-of-the-maximum-value-in-a-numpy-array/.
stats writer (2024) 'How can I find the index of the maximum value in a NumPy array?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-the-index-of-the-maximum-value-in-a-numpy-array/.
[1] stats writer, "How can I find the index of the maximum value in a NumPy array?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I find the index of the maximum value in a NumPy array?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
