How to Use NumPy argmax in Python

NumPy argmax()in Python is used to return the indices of the max elements of the given array along with the specified axis. Using this function gets the indices of maximum elements of single dimensions and multi-dimensional(row-wise or column-wise) of the given array.

In this article, I will explain with examples how to use the NumPy agrmax() function and return the index of the maximum values from the given array with examples.

1. Quick Examples of Python NumPy argmax() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy argmax() function. To Learn NumPy with examples refer to NumPy Tutorial.


# Below are a quick example

# Example 1: Index with the largest value
arr = np.array([4,7,2,9,12,16,19])
arr2 = np.argmax(arr)

# Example 2: The largest value in the array
arr3 = arr[arr2]

# Example 3: # Create an array using arange()
arr = np.arange(6).reshape(2,3)+10
Get the index of the highest value in a 2-D array 
arr1 = np.argmax(arr)


# Example 4: Use numpy.argmax() function to set axis Parameter
arr1 = np.argmax(arr,axis=0)

# Example 5: Get indices of the highest elements along the row axis
arr1 = np.argmax(arr,axis=1)

# Example 6: Set out parameter in numpy.argmax() function
arr1 = np.array(0) 
np.argmax(arr, out=arr1)

2. Syntax of NumPy argmax()

Following is the syntax of the argmax() function.


# Syntax of python argmax() 
numpy.argmax(arr, axis=None, out=None)

2.1 Parameters of argmax()

Following are the parameters of argmax() function.

  • arr – Input array.
  • axis – By default is None, the index is into the flattened array, otherwise along the specified axis like 0 or 1.
  • out – Provided the result will be inserted output to the out array. It should be of the appropriate shape and type.

2.2 Return Value of argmax()

It returns an array of the same shape of the given array which contains the indices of the maximum elements.

3. Usage of argmax() NumPy Function

numpy.argmax() is available in the NumPy module package in Python and is used to get an array of indices of max values of a given array of single dimensions or multi-dimensional. By default, it returns the max of the flattened array otherwise, along with the specified axis. Use argsort() to sort the array elements and get the indices of sorted elements.

Lets create 1- Dimensional NumPy array using numpy.array() function and find the max value index using this function, it will return the index of the max elements of the given array.


import numpy as np
arr = np.array([4,7,2,9,12,16,19])

# Get the index of largest value
arr1 = np.argmax(arr)
print(arr1)

# Output
# 6

# Get the largest value in the array
arr2 = arr[arr1]
print(arr2)

# Output
# 19

4. Get the Index Max Value of 2-D Array

To get the index of the highest value in a 2-D array use this function, Let’s create 2-D NumPy array using numpy.arange() function. Since we are not using axis param here, it returns the max value index from the flattened array.


# Create an array using arange()
arr = np.arange(6).reshape(2,3)+10
print("Input array:", arr)

# Get the index of the highest value in a 2-D array 
arr1 = np.argmax(arr)
print("Index of max element:",arr1)

# Output
# Input array: [[10 11 12]
# [13 14 15]]
# Index of max element: 5

5. Get the ArgMax of NumPy Index along axis = 0

By setting axis=0 parameter, it gives indices of the highest value along each column. The first column has the highest value in index 1, the second column has the highest value at index 1 and the third column has the highest value at index 0.


arr = np.array([[1,5,7],[2,6,4]])
print("Input array:", arr)

arr2 = np.argmax(arr,axis=0)
print("Result:",arr2)

# Output
#Input array: [[1 5 7]
# [2 6 4]]
#Result: [1 1 0]

6. Get the NumPy ArgMax Index along axis = 0

By setting axis=1 parameter, it gives indices of the highest value along each row.


arr = np.array([[1,5,7],[2,6,4]])
print("Input array:", arr)

arr2 = np.argmax(arr,axis=1)
print("Result:",arr2)

# Output
#Input array: [[1 5 7]
# [2 6 4]]
#Result: [2 1]

7. Set Out Parameter in NumPy argmax() Function

We can also pass the out parameter of the same shape and same type of given array and find the index of the maximum element of the given array. It sets the output into an array provided with out param.


# Set out parameter in numpy.argmax() function
arr1 = np.array(0) 
np.argmax(arr,out=arr1)
print("Result:",arr1)

# Output
# 2

8. Conclusion

In this article, I have explained how to use the numpy agrmax() function and how to get the index of the maximum value from the given array of single or multi-dimensional with examples.

Happy Learning!!

You May Also Like

References

Leave a Reply

You are currently viewing How to Use NumPy argmax in Python