NumPy ndarray.min()
The numpy.ndarray.min()
method returns the minimum value in a NumPy array.
It can operate across the entire array or along a specified axis.
Syntax
</>
Copy
ndarray.min(axis=None, out=None, keepdims=False, initial=<no value>, where=True)
Parameters
Parameter | Type | Description |
---|---|---|
axis | None, int, or tuple of ints, optional | Axis or axes along which the minimum is computed. If None , the minimum of the entire array is returned. |
out | ndarray, optional | Alternative output array for storing the result. Must have the same shape as the expected output. |
keepdims | bool, optional | If True , retains reduced dimensions as size one, allowing proper broadcasting. |
initial | scalar, optional | Initial value to compare against when computing the minimum. If provided, the minimum is at least this value. |
where | array_like of bool, optional | Specifies elements to include in the computation. |
Return Value
Returns a scalar value if axis=None
, or an array of minimum values if an axis is specified.
The result is the smallest element (or smallest element along the specified axis).
Examples
1. Finding the Minimum Value in an ndarray
This example demonstrates how to find the minimum value in a NumPy array.
</>
Copy
import numpy as np
# Creating a 2D NumPy array
arr = np.array([[4, 7, 1],
[9, 3, 6]])
# Finding the minimum value in the entire array
result = arr.min()
print("Minimum value in the array:", result)
Output:
Minimum value in the array: 1
2. Using the axis
Parameter in ndarray.min()
Here, we compute the minimum along different axes.
</>
Copy
import numpy as np
# Creating a 2D NumPy array
arr = np.array([[8, 2, 5],
[1, 7, 3]])
# Finding the minimum value along axis 0 (column-wise)
result_axis0 = arr.min(axis=0)
print("Minimum along axis 0 (columns):", result_axis0)
# Finding the minimum value along axis 1 (row-wise)
result_axis1 = arr.min(axis=1)
print("Minimum along axis 1 (rows):", result_axis1)
Output:
Minimum along axis 0 (columns): [1 2 3]
Minimum along axis 1 (rows): [2 1]
3. Keeping Dimensions with keepdims=True
in ndarray.min()
Using keepdims=True
ensures the output retains the reduced dimension.
</>
Copy
import numpy as np
# Creating a 2D NumPy array
arr = np.array([[5, 9, 2],
[6, 4, 8]])
# Finding the minimum along axis 1 while keeping dimensions
result = arr.min(axis=1, keepdims=True)
print("Minimum values with kept dimensions:\n", result)
Output:
Minimum values with kept dimensions:
[[2]
[4]]
4. Using the initial
Parameter in ndarray.min()
The initial
parameter sets a minimum starting value.
</>
Copy
import numpy as np
# Creating a 1D NumPy array
arr = np.array([10, 15, 3, 7])
# Finding the minimum value with an initial comparison value of 5
result = arr.min(initial=5)
print("Minimum value with initial=5:", result)
Output:
Minimum value with initial=5: 3
5. Using the where
Parameter in ndarray.min()
The where
parameter allows computing the minimum only for selected elements.
</>
Copy
import numpy as np
# Creating a 1D NumPy array
arr = np.array([12, 7, 19, 5])
# Creating a boolean mask
mask = np.array([True, False, True, True])
# Finding the minimum only for elements where mask is True
result = arr.min(initial=100, where=mask)
print("Minimum value considering only masked elements:", result)
Output:
Minimum value considering only masked elements: 5