Table of Contents
ToggleIn mathematics, the “max” (maximum) refers to the largest value in a set of numbers. It identifies the greatest element, providing a measure of the highest point in a data set.
For example, in the set {3, 1, 4, 2}, the maximum is 4. The maximum is useful for understanding the upper bound of a data set.
The max() function in NumPy returns the largest value in an array. It can be applied to the entire array or along a specified axis to find the maximum value in each row or column.
You can also use the amax() function, which is an alias for max() function. Following is the basic syntax of the max() function in NumPy −
numpy.max(a, axis=None, out=None, keepdims=False)
Where,
The calculation of the maximum value in a dataset is simple and easy. The function scans through all the elements in the array and identifies the largest value. This process can be applied to arrays of any shape or size.
Let us understand this concept with an example −
# Open Compiler
import numpy as np
data = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])
# Calculating the maximum value
max_value = np.max(data)
print("Maximum value:", max_value)
Output:
This will produce the following result −
Maximum value: 9
In NumPy, the axis parameter allows you to compute the maximum value along specific axes of a multi-dimensional array. The axis parameter refers to the direction along which the maximum value should be calculated. For example, in a 2D array −
In the following example, we are computing the maximum value along both axes of the 2D array −
# Open Compiler
import numpy as np
# Create a 2D array
data_2d = np.array([[1, 3, 5], [2, 4, 6], [7, 8, 9]])
# Calculate the maximum value along axis 0 (columns)
max_axis_0 = np.max(data_2d, axis=0)
# Calculate the maximum value along axis 1 (rows)
max_axis_1 = np.max(data_2d, axis=1)
print("Maximum value along axis 0:", max_axis_0)
print("Maximum value along axis 1:", max_axis_1)
Output:
In the output below, the maximum value along axis 0 is computed by finding the largest element in each column. The maximum value along axis 1 is calculated by finding the largest element in each row −
Maximum value along axis 0: [7 8 9]
Maximum value along axis 1: [5 6 9]
The numpy.max() function also works for arrays with more than two dimensions. You can specify the axis along which to calculate the maximum value, and the function will return the maximum value for that axis while retaining the other dimensions. If no axis is specified, the maximum value is calculated over the entire array.
Following is an example to compute maximum value of a 3D array −
# Open Compiler
import numpy as np
# Create a 3D array
data_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
# Maximum value along axis 0
max_3d_axis_0 = np.max(data_3d, axis=0)
# Maximum value along axis 1
max_3d_axis_1 = np.max(data_3d, axis=1)
# Maximum value along axis 2
max_3d_axis_2 = np.max(data_3d, axis=2)
print("Maximum value along axis 0:", max_3d_axis_0)
print("Maximum value along axis 1:", max_3d_axis_1)
print("Maximum value along axis 2:", max_3d_axis_2)
Output:
In this case, the maximum value is calculated along each of the axes (0, 1, and 2) for the 3D array. The function returns the maximum values for each of the specified axes while preserving the other dimensions −
Maximum value along axis 0: [[5 6]
[7 8]]
Maximum value along axis 1: [[3 4]
[7 8]]
Maximum value along axis 2: [[2 4]
[6 8]]
Sometimes, arrays may contain NaN (Not a Number) values, which can interfere with the calculation of the maximum value. To handle NaN values, NumPy provides an option to ignore them during the max calculation.
You can use the numpy.nanmax() function, which computes the maximum value while ignoring NaN values.
In this example, we are handling NaN values while computing maximum value in NumPy −
# Open Compiler
import numpy as np
# Create an array with NaN values
data_with_nan = np.array([1, 3, np.nan, 5, 7])
# Calculate the maximum value while ignoring NaN values
max_without_nan = np.nanmax(data_with_nan)
print("Maximum value without NaN:", max_without_nan)
Output:
After executing the above code, we get the following output −
Maximum value without NaN: 7.0
The out parameter in the numpy.max() function allows you to store the result of the maximum value computation in a pre-allocated array.
This can be useful for memory management and efficiency when working with large datasets. The result is stored in the array specified by the out parameter, which must have the same shape as the expected output.
In this example, the maximum value of the array data is calculated and stored in the pre-allocated array out_array, which is then printed to show the result −
# Open Compiler
import numpy as np
# Create an array
data = np.array([5, 2, 9, 1, 5, 6])
# Create an output array
out_array = np.empty((), dtype=np.int32)
# Calculate the maximum value and store it in out_array
np.max(data, out=out_array)
print("Output array:", out_array)
Output:
The result produced is as follows −
Output array: 9
Key Takeaway: Master the NumPy max function at Vista Academy!
