How to Use min and max Functions in NumPy for Data Analysis
Use
numpy.min() to find the smallest value and numpy.max() to find the largest value in a NumPy array. These functions can operate on the entire array or along a specific axis to get min or max values per row or column.Syntax
The basic syntax for finding minimum and maximum values in NumPy arrays is:
numpy.min(array, axis=None): Returns the smallest value in the array or along the specified axis.numpy.max(array, axis=None): Returns the largest value in the array or along the specified axis.
Parameters:
array: The NumPy array to analyze.axis: Optional. Specify 0 for columns, 1 for rows, or None for the whole array.
python
import numpy as np # Find min and max of entire array np.min(array) np.max(array) # Find min and max along rows (axis=1) or columns (axis=0) np.min(array, axis=1) np.max(array, axis=0)
Example
This example shows how to find the minimum and maximum values in a 2D NumPy array overall and along each axis.
python
import numpy as np array = np.array([[3, 7, 5], [1, 6, 9], [4, 2, 8]]) # Minimum and maximum of entire array min_val = np.min(array) max_val = np.max(array) # Minimum and maximum along columns (axis=0) min_col = np.min(array, axis=0) max_col = np.max(array, axis=0) # Minimum and maximum along rows (axis=1) min_row = np.min(array, axis=1) max_row = np.max(array, axis=1) print(f"Min overall: {min_val}") print(f"Max overall: {max_val}") print(f"Min per column: {min_col}") print(f"Max per column: {max_col}") print(f"Min per row: {min_row}") print(f"Max per row: {max_row}")
Output
Min overall: 1
Max overall: 9
Min per column: [1 2 5]
Max per column: [4 7 9]
Min per row: [3 1 2]
Max per row: [7 9 8]
Common Pitfalls
Common mistakes when using numpy.min() and numpy.max() include:
- Forgetting to specify
axiswhen you want min/max per row or column, which returns a single value for the whole array. - Using the wrong axis number (0 is columns, 1 is rows), which can lead to unexpected results.
- Passing non-NumPy arrays or incompatible types, causing errors.
Always check the shape of your array and what axis you want to operate on.
python
import numpy as np array = np.array([[10, 20], [30, 40]]) # Wrong: expecting min per row but forgot axis wrong_min = np.min(array) # returns 10, min of whole array # Right: specify axis=1 for min per row right_min = np.min(array, axis=1) # returns [10 30] print(f"Wrong min (no axis): {wrong_min}") print(f"Right min (axis=1): {right_min}")
Output
Wrong min (no axis): 10
Right min (axis=1): [10 30]
Quick Reference
| Function | Description | Axis Parameter |
|---|---|---|
| numpy.min(array, axis=None) | Returns smallest value in entire array or along axis | None (whole array), 0 (columns), 1 (rows) |
| numpy.max(array, axis=None) | Returns largest value in entire array or along axis | None (whole array), 0 (columns), 1 (rows) |
Key Takeaways
Use numpy.min() and numpy.max() to find smallest and largest values in arrays.
Specify the axis parameter to get min/max per row or column.
Axis=0 means operate along columns; axis=1 means operate along rows.
Without axis, min/max returns a single value for the whole array.
Always check your array shape and axis to avoid unexpected results.