Python numpy min

The Python numpy min() function finds the minimum element in a given array along the specified axis.

Syntax

The syntax of this statistical Python numpy min() method is

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

Here, a is a ndarray that is mandatory, and all the other arguments are optional.

Python numpy min Example

In this example, we declare a one-dimensional ndarray, and it will find the minimum item among them. 

import numpy as np

a = np.array([5, 11, 1, 99, 77, 6])

b  = np.min(a)
print(b)
1

Two-Dimensional example

In this program, we will declare a two-dimensional square matrix to find the least value among all the ndarray rows and columns.

import numpy as np

a = np.array([[40, 7], [3, 50]])

b  = np.min(a)
print(b)
3

Python numpy min Row and Column Wise

If there is no axis argument, this min() method will find and return the minimum element irrespective of the dimensions or size. However, we can compute the row and column-wise minimum values by specifying the axis value. For instance, in the below example, axis = 0 prints the minimum item in each column, and axis = 1 prints for each row of the two-dimensional array.

The keepdims argument accepts boolean True or False, and the default value is false. Therefore, the result will reshape if you use the keepdims argument and assigned True value.

Here, the c variable finds the minimum in each column, and the d variable finds each row.

import numpy as np

a = np.random.randint(10, 80, size = (5, 4))
print(a)

b  = np.min(a)
print(b)

c = np.min(a, axis = 0)
print(c)

d = np.min(a, axis = 1)
print(d)

e = np.min(a, axis = 1,  keepdims = True)
print(e)
[[31 25 14 12]
 [13 41 25 54]
 [79 74 24 52]
 [20 18 53 56]
 [30 43 54 62]]

12

[13 18 14 12]

[12 13 24 18 30]

[[12]
 [13]
 [24]
 [18]
 [30]]

out argument

Use the out variable to save the min() method result in an array. Remember, the variable size has to match the output. In this example, np.min(a, axis = 1, out = x) will save the result in x array.

import numpy as np

a = np.random.randint(10, 80, size = (5, 4))
print(a)

x = np.arange(5)

b = np.min(a, axis = 1)
print(b)

np.min(a, axis = 1, out = x)
print('x = ',  x)
[[38 13 35 78]
 [79 59 33 71]
 [51 15 53 21]
 [60 23 46 32]
 [74 42 20 31]]

[13 33 15 23 20]

x =  [13 33 15 23 20]

where argument

To use the where argument, you have to use the initial; otherwise, the Python numpy min function will throw an error. For example, we used the where argument along the axis in the below code.

import numpy as np

a = np.array([[2, 5, 3], [4, 6, 7], [1, 2, 9]])
print(a)

b  = np.min(a)
print(b)

c = np.min(a, where = [False, True, True], initial = 10)
print(c)

d = np.min(a, where = [False, True, True], axis = 0, initial = 10)
print(d)
[[2 5 3]
 [4 6 7]
 [1 2 9]]
1
2
[10  2  3]

where = [False, True, True], initial = 10 – It will check the 2 and 3 columns and omits the first column values to find the minimum value.

where = [False, True, True], axis = 0, initial = 10 – Because the first column is False, the minimum value becomes 10 (taken from the initial argument). Suppose you change the initial value to 2 for the third column. In that case, it compares the initial value (2) with column values (3, 7, and 9) and returns 2 as the output.

import numpy as np

a = np.array([[2, 5, 3], [4, 6, 7], [1, 2, np.nan]])
print(a)

b  = np.min(a)
print(b)

c = np.min(a, where = ~np.isnan(a), initial = 10)
print(c)
[[ 2.  5.  3.]
 [ 4.  6.  7.]
 [ 1.  2. nan]]

nan

1.0