np.max() mirrors np.min() exactly, but for the largest value — it works over the whole array by default, or along a specified axis for multi-dimensional arrays, collapsing that axis into an array of per-position maximums. The element-wise, two-array counterpart is np.maximum(a, b), which compares two arrays position by position and keeps the larger value at each one, distinct from a single-array reduction.
1Understanding np.max()
np.max() mirrors np.min() exactly, but for the largest value — it works over the whole array by default, or along a specified axis for multi-dimensional arrays, collapsing that axis into an array of per-position maximums. The element-wise, two-array counterpart is np.maximum(a, b), which compares two arrays position by position and keeps the larger value at each one, distinct from a single-array reduction.
Just like with min, use np.maximum(a, b) for an element-wise comparison between two arrays, and reserve np.max() for finding the largest value within, or along an axis of, a single array.
import numpy as np
arr = np.array([5, 2, 8, 1, 9])
print(np.max(arr))2Practical Example
Here is a real-world application of np.max() showing how it is used in production NumPy code.
import numpy as np
matrix = np.array([[1, 5], [3, 2]])
print(np.max(matrix, axis=1))3Best Practices
Follow these guidelines when working with np.max():
1. Use np.max()/arr.max() for the overall or per-axis largest value within one array
2. Use np.maximum(a, b) instead when you need an element-wise comparison between two separate arrays
3. Use np.nanmax() when NaN values in the data should be ignored rather than causing the result to be NaN
Tip: Just like with min, use np.maximum(a, b) for an element-wise comparison between two arrays, and reserve np.max() for finding the largest value within, or along an axis of, a single array.
import numpy as np
arr = np.array([5, 2, 8, 1, 9])
print(np.max(arr))