Find Maximum Value in NumPy Array along an Axis
NumPy - Maximum value in array along an axis
You can find the maximum or largest value of a Numpy array, not only in the whole numpy array, but also along a specific axis or set of axes.
To get the maximum value of a NumPy Array along an axis, use numpy.amax()
function.
Syntax
The syntax of numpy.amax()
function is as follows:
max_value = numpy.amax(arr, axis)
If you do not provide any axis, the maximum of the entire array is returned. You can also specify an axis or axes along which to operate. If the axis is a tuple of integers representing the axes, then the maximum is selected over these specified multiple axes.
Examples
1. Maximum value along an axis
In this example, we will take a numpy array with random integers and then find the maximum of the array along an axis using numpy.amax()
function. We shall find the maximum value along axis=0
and axis=1
separately.
Python Program
import numpy as np
# 2D array => 2 axes
arr = np.random.randint(10, size=(4,5))
print('array\n', arr)
#find maximum value along axis=0
amax_value = np.amax(arr, axis=0)
print('Maximum value of the array along axis=0')
print(amax_value)
#find maximum value along axis=1
amax_value = np.amax(arr, axis=1)
print('Maximum value of the array along axis=1')
print(amax_value)
Explanation:
- The
np.random.randint()
function generates a 4x5 matrix with random integers between 0 and 9. - Using
np.amax(arr, axis=0)
, the function finds the maximum value for each column (along the vertical axis). - Using
np.amax(arr, axis=1)
, the function finds the maximum value for each row (along the horizontal axis). - The results are printed for both axis 0 and axis 1.
Output
array
[[4 3 0 0 9]
[7 5 7 0 5]
[2 8 1 8 7]
[3 8 5 0 2]]
Maximum value of the array along axis=0
[7 8 7 8 9]
Maximum value of the array along axis=1
[9 7 8 8]
2. Maximum value along multiple axes
As mentioned earlier, we can calculate the maximum value along multiple axes by providing a tuple of axes. In the example below, we will find the maximum value along both axis 0 and axis 2.
Python Program
import numpy as np
# 3D array => 3 axes
arr = np.random.randint(9, size=(2,2,4))
print(arr)
# find maximum value along axis=0,2
amax_value = np.amax(arr, axis=(0, 2))
print('Maximum value of the array along axis=(0,2)')
print(amax_value)
Explanation:
- The array is created using
np.random.randint()
to generate random integers between 0 and 8 in a 2x2x4 matrix. np.amax(arr, axis=(0, 2))
calculates the maximum values along both axis 0 (depth) and axis 2 (columns).- The result is a 1D array showing the maximum value for each slice along the specified axes.
Output
array
[[[4 3 6 5]
[4 8 8 3]]
[[5 3 1 2]
[8 0 2 5]]]
Maximum value of the array along axis=(0,2)
[6 8]
3. Maximum Value in a 1D Array
We can also find the maximum value in a 1D array, where only the array itself is considered, and the concept of axis doesn't apply.
Python Program
import numpy as np
# 1D array
arr = np.random.randint(20, size=5)
print('array\n', arr)
# find maximum value in the 1D array
amax_value = np.amax(arr)
print('Maximum value of the array')
print(amax_value)
Explanation:
- The array is created using
np.random.randint()
to generate random integers between 0 and 19 in a 1D array. np.amax(arr)
is used to find the maximum value in the entire array as no axis is provided.- The result shows the maximum value present in the array.
Output
array
[11 2 8 13 6]
Maximum value of the array
13
Summary
In this Numpy Tutorial, we explored how to find the maximum value in a Numpy array along an axis or multiple axes using the numpy.amax()
function. We covered several use cases with detailed explanations and examples, including working with 1D, 2D, and 3D arrays.