We use np.min() and np.max() to find the smallest and largest values in a group of numbers. This helps us understand the range of data quickly.
0
0
np.min() and np.max() in NumPy
Introduction
Finding the lowest and highest temperatures recorded in a week.
Checking the minimum and maximum scores in a class test.
Identifying the smallest and largest sales numbers in a month.
Determining the range of heights in a group of people.
Syntax
NumPy
np.min(array, axis=None) np.max(array, axis=None)
array is the data you want to check, like a list or NumPy array.
axis lets you find min or max along rows (axis=1) or columns (axis=0) in 2D arrays.
Examples
Finds the smallest number in the list, which is 1.
NumPy
np.min([3, 1, 4, 2])
Finds the largest number in the list, which is 4.
NumPy
np.max([3, 1, 4, 2])
Finds the smallest numbers in each column: [1, 2].
NumPy
arr = np.array([[1, 5], [3, 2]]) np.min(arr, axis=0)
Finds the largest numbers in each row: [5, 3].
NumPy
arr = np.array([[1, 5], [3, 2]]) np.max(arr, axis=1)
Sample Program
This program shows how to find the smallest and largest scores in a set of test results. It finds the overall min and max, then finds min scores per test and max scores per student.
NumPy
import numpy as np # Create a 2D array of test scores scores = np.array([[88, 92, 79], [95, 85, 91], [78, 90, 86]]) # Find the minimum score overall min_score = np.min(scores) # Find the maximum score overall max_score = np.max(scores) # Find minimum score in each test (column) min_per_test = np.min(scores, axis=0) # Find maximum score for each student (row) max_per_student = np.max(scores, axis=1) print(f"Minimum score overall: {min_score}") print(f"Maximum score overall: {max_score}") print(f"Minimum score per test: {min_per_test}") print(f"Maximum score per student: {max_per_student}")
OutputSuccess
Important Notes
If you don't use axis, np.min() and np.max() look at all values in the array.
Using axis=0 checks down columns, axis=1 checks across rows in 2D arrays.
Summary
np.min() finds the smallest value in data.
np.max() finds the largest value in data.
You can find min or max overall or along rows/columns using axis.