NumPy.argmax()
numpy.argmax()
is a NumPy library function that returns indices of the maximum values along an axis. It is used to return the index of the largest element in an array.
Syntax
numpy.argmax(arr, axis=None, out=None)
- arr: An array containing numbers from which the function will evaluate
- axis: An integer representing the axis along which we compare the elements
- out: Array to fill with result. It is optional.
Example
import numpy as np
arr = np.array([1, 3, 7, 9, 5])
max_index = np.argmax(arr)
print("The index of the maximum element is: ", max_index)
Output:
The index of the maximum element is: 3
Explanation
The numpy.argmax()
function returns the index of the largest element in the array. In the above example, we created a NumPy array arr
that contains 5 integers. We used np.argmax()
to find the index of the maximum element of arr
. The output of the program returns the index where the largest element, number 9, is at the index number 3.
Use
The numpy.argmax()
function can be used to find the index of the largest element in an array. It can be used in scientific and mathematical computations.
Important Points
- If
axis
is not set, the index of the largest element in the flattened array is returned. - If the array contains multiple elements with the maximum value, the first occurrence will be returned.
- Multi-dimensional arrays can be used with
axis
to get the maximum value along a particular axis.
Summary
numpy.argmax()
function is used to return the index of the largest element in the array. It is used to find the maximum value in the array and its position in the array. It can be used with scientific and mathematical computations that require searching for the largest element in an array.