Python:NumPy min()
The ndarray.min() method returns the smallest value in a NumPy array. It can find the minimum of the entire array or along a specific axis. The result is always returned as a scalar or a new array depending on the axis used.
Syntax
ndarray.min(axis=None, out=None, keepdims=False, initial=<no value>, where=True)
Parameters:
axis(optional): Axis along which the minimum is computed.None(default): Computes the minimum of the entire array.0: Computes the column-wise minimum of the entire array.1: Computes the row-wise minimum of the entire array.
out(optional): Output array to store the result. Must have the correct shape.keepdims(bool): IfTrue, retains reduced dimensions with size 1.initial(optional): Starting value used for comparison before array elements are considered.where(array_like of bool): Condition that selects elements to be included in the reduction.
Return value:
A scalar or array containing the minimum values. If an axis is specified, the result is returned with one fewer dimension unless keepdims=True is used.
Example 1: Minimum of an Entire Array
This example computes the minimum of a one-dimensional array:
import numpy as nparr = np.array([12, 5, 30, 2, 19])result = arr.min()print(result)
The output of this code is:
2
Example 2: Minimum Along an Axis
This example finds the smallest value in each row:
import numpy as nparr = np.array([[4, 7, 1],[9, 3, 2]])result = arr.min(axis=1)print(result)
The output of this code is:
[1 2]
Codebyte Example
This example calculates the overall minimum and row-wise minimums:
Frequently Asked Questions
1. What is a NumPy ndarray in Python?
NumPy ndarray is a multidimensional array object used for efficient numerical computing.
2. What is the min() function in NumPy?
min() is a reduction method that returns the smallest value in a NumPy array or along a specific axis.
3. What is %s and %d in Python?
%s and %d are old-style string formatting placeholders:
%s: Inserts a string.%d: Inserts an integer.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
- Includes 27 Courses
- With Professional Certification
- Beginner Friendly.95 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours