NumPy min()

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

Example

import numpy as np

array1 = np.array([10, 12, 14, 11, 5])

# return the smallest element minValue= np.min(array1)
print(minValue) # Output: 5

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.
import numpy as np

array = np.array([[10, 17, 25], 
                  [15, 11, 22]])
                  
# return the smallest element of the flattened array minValue = np.min(array)
print('The smallest element in the flattened array: ', minValue)
# return the smallest element in each column minValue = np.min(array, axis = 0)
print('The smallest element in each column (axis 0): ', minValue)
# return the smallest element in each row minValue = np.min(array, axis = 1)
print('The smallest element in each row (axis 1): ', minValue)

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.

import numpy as np

array1 = np.array([[10, 17, 25], 
                   [15, 11, 22], 
                   [11, 19, 20]])

# create an empty array
array2= np.array([0, 0, 0])

# pass the 'out' argument to store the result in array2 np.min(array1, axis = 0, out = array2)
print(array2)

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.

import numpy as np

array1 = np.array([[10, 17, 25], 
              	    [15, 11, 22]])

print('Dimensions of original array: ', array1.ndim)

minValue = np.min(array1, axis = 1)
print('\n Without keepdims: \n', minValue) print('Dimensions of array: ', minValue.ndim)
# set keepdims to True to retain the dimension of the input array minValue = np.min(array1, axis = 1, keepdims = True)
print('\n With keepdims: \n', minValue) print('Dimensions of array: ', minValue.ndim)

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.

import numpy as np

# min value < initial, returns min value
array1 = np.array([[10, 25, 17, 16, 14]])
minValue = np.min(array1, initial = 16)
print(minValue) # min value > initial, returns initial array2 = np.array([[10, 25, 17, 16, 14]])
minValue = np.min(array2, initial = 6)
print(minValue) # in case of an empty array, initial value is returned array3 = np.array([])
minValue = np.min(array3, initial = 5)
print(minValue)

Output

10
6
5.0

Example 5: min() With where Argument

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

import numpy as np

arr = np.array([[12, 25, 32],
              	[47, 50, 36]])

# take min of entire array result1 = np.min(arr) # min of only odd elements result2 = np.min(arr, initial = 50, where = (arr%2==1)) # min of numbers greater than 30 result3 = np.min(arr, initial = 50,where = (arr > 30))
print('min of entire array:', result1) print('min of only odd elements:', result2) print('min of numbers greater than 30:', result3)

Output

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