NumPy min()

The min() method returns the smallest element of an array along an axis.


min() Syntax

The syntax of min() is:

numpy.min(array, axis = None, out = None, keepdims = <no value>, initial=<no value>, where=<no value>)

min() Arguments

The min() method takes six arguments:

  • array - input array
  • axis (optional) - axis along which minimum value is returned (int)
  • out (optional) - array to store the output
  • keepdims (optional) - whether to preserve the input array's dimension (bool)
  • initial (optional) - the minimum value of an output element (scalar)
  • where (optional) - elements to include in the minimum value calculation(array of bool)

min() Return Value

The min() method returns the smallest element.

Note: If at least one element of the input array is NaN, min() will return NaN.


Example 1: min() With 2D Array

The axis argument defines how we can handle the smallest element in a 2D array.

  • If axis = None, the array is flattened and the minimum of the flattened array is returned.
  • If axis = 0, the smallest element in each column is returned.
  • If axis = 1, the smallest element in each row is returned.

Output

The smallest element in the flattened array:  10
The smallest element in each column (axis 0):  [10 11 22]
The smallest element in each row (axis 1):  [10 11]

Example 2: Use out to Store the Result in Desired Location

In our previous examples, the min() function generated a new output array.

However, we can use an existing array to store the output using the out argument.

Output

[10 11 20]

Example 3: min() With keepdims

When keepdims = True, the dimensions of the resulting array matches the dimension of an input array.

Output

Dimensions of original array:  2

 Without keepdims: 
[10 11]
Dimensions of array:  1

 With keepdims: 
 [[10]
 [11]]
Dimensions of array:  2

Without keepdims, the result is simply a one-dimensional array of smallest numbers.

With keepdims, the resulting array has the same number of dimensions as the input array.


Example 4: min() With initial

We use initial to define the maximum value np.min() can return. If the minimum value of the array is larger than the initial value, initial is returned.

Output

10
6
5.0

Example 5: where to Find Minimum of Filtered Array

The optional argument where specifies elements to include in the calculation of minimum value.

Output

min of entire array: 12
min of only odd elements: 25
min of numbers greater than 30: 32