• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:16 mins read
You are currently viewing 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 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.


# Quick examples of argmax() function

# Example 1: Get the index of largest value
arr = np.array([4,7,2,9,12,16,19])
arr1 = np.argmax(arr)

# Example 2: Get the largest value in the array
arr2 = arr[arr1]

# Example 3: Get the index of the maximum value 
# In the flattened 2D array
arr = np.arange(6).reshape(2,3)+10
arr1 = np.argmax(arr)

# Example 4: Find the indices of the maximum values 
# Along axis 0 (columns)
arr = np.array([[1,5,7],[2,6,4]])
arr2 = np.argmax(arr,axis=0)

# Example 5: Find the indices of the maximum values 
# Along axis 1 (rows)
arr = np.array([[1,5,7],[2,6,4]])
arr2 = np.argmax(arr,axis=1)

# Example 6: Set out parameter 
# Use numpy.argmax() function
arr = np.array([[1,5,7],[2,6,4]])
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 – This is the input array for which you want to find the indices of the maximum values.
  • 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()

The argmax() method returns an array of indices of the maximum values. If multiple occurrences of the maximum value exist, only the index of the first occurrence is returned.

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.

3.1 Get the Index of the Max Value of 1D Array

To find the index of the maximum value in a 1D NumPy array, you can use the numpy.argmax() function. In this program, np.argmax(arr) returns the index of the maximum value in the arr array. The result is then printed, and you will see the index of the maximum value in the output.

Keep in mind that if there are multiple occurrences of the maximum value, argmax returns the index of the first occurrence. If you want to find all occurrences, you may need to use additional functions like np.where.


# Import numpy
import numpy as np

# Create a 1D NumPy array
arr = np.array([4,7,2,9,12,16,19])
print("Original array:", arr)

# Get the index of largest value
arr1 = np.argmax(arr)
print("Index of the maximum value:", arr1)

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

Yields below output.

numpy argmax

4. Get the Index of the Max Value of 2D 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 you 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("Original array:\n", arr)

# Get the index of the maximum value 
# In the flattened 2D array
arr1 = np.argmax(arr)
print("Index of the maximum value in the flattened array:\n", arr1)

Yields below output.

numpy argmax

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

To find the index of the maximum value along a specific axis (axis=0 for columns) in a 2D NumPy array, you can use the numpy.argmax() function with the axis parameter set to 0.

In the below example, the argmax() function is applied along axis 0, and it returns the indices of the maximum values along each column. The result will be a 1D array containing the indices of the maximum values for each column.


# Create a 2D NumPy array
arr = np.array([[1,5,7],[2,6,4]])
print("Input array:\n", arr)

# Find the indices of the maximum values 
# Along axis 0 (columns)
arr2 = np.argmax(arr,axis=0)
print("Indices of the maximum values along axis 0:\n", arr2)

# Output:
# Input array:
#  [[1 5 7]
#  [2 6 4]]
# Indices of the maximum values along axis 0:
#  [1 1 0]

6. Get the ArgMax of NumPy Index along axis = 1

To find the index of the maximum value along a specific axis (axis=1 for rows) in a 2D NumPy array, you can use the numpy.argmax() function with the axis parameter set to 1.

In the below example, np.argmax(arr, axis=1) returns an array containing the indices of the maximum values along each row. So, for the first row, the maximum value is at index 2, and for the second row, the maximum value is at index 1.


# Create a 2D NumPy array
arr = np.array([[1,5,7],[2,6,4]])
print("Input array:\n", arr)

# Find the indices of the maximum values 
# Along axis 1 (rows)
arr2 = np.argmax(arr,axis=1)
print("Indices of the maximum values along axis 1:\n", arr2)

# Output:
# Input array:
#  [[1 5 7]
#  [2 6 4]]
# Indices of the maximum values along axis 1:
#  [2 1]

7. Set Out Parameter in NumPy argmax() Function

You 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

Frequently Asked Questions

What does argmax do in NumPy?

The argmax is a NumPy function that returns the indices of the maximum values along a specified axis in a NumPy array. The term “argmax” stands for “argument of the maximum,” which refers to the index position of the maximum value in the array.

What is NumPy’s argmax function used for?

The argmax function in NumPy is used to find the indices of the maximum values along an axis in a NumPy array. It returns the indices of the maximum values, allowing you to identify the position of the maximum element in an array.

How does argmax work on a 1D array?

When applied to a 1D array, argmax returns the index of the maximum element in that array. The index is the position of the first occurrence of the maximum value in the array.

Can argmax be applied to multi-dimensional arrays?

argmax can be applied to multi-dimensional arrays. When working with multi-dimensional arrays, you can specify the axis along which the maximum values are found. This allows you to find the indices of maximum values along a specific dimension.

Is it possible to use argmax with other data types besides integers?

argmax can be used with arrays of various data types, including floating-point numbers. The function compares elements based on their values, regardless of the data type.

What happens if the input array is empty?

If the input array is empty, argmax will raise a ValueError because there are no elements to find the maximum index.

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!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.