numpy.min(a,axis=None,out=None,keepdims, initial, where)
Return min of elements across given axis.
a | array, elements to get the min value |
axis | Int (optional ), or tuple, default is None, minimum value among all the elements. If axis given then values across the axis is returned. |
out | Optional. If given then output to be stored. Must be of same shape as of the output |
keepdims | Bool ( Optional ), output matches to the input array dimension. |
where | Optional, Elements to include for calculation of minimum value |
initial | Optional, int, Minimum value of the output. If given then this is considered if it is less than the actual output |
import numpy as np
# my_data=np.random.randint(2,high=7,size=(3,3),dtype='int16')
my_data=np.array([[6, 3, 2], [7, 2, 2], [6, 2, 9]])
print(my_data)
Output
[[6 3 2]
[7 2 2]
[6 2 9]]
print("min() : ", my_data.min())
print("min(axis=0): ", my_data.min(axis=0))
print("min(axis=1): ", my_data.min(axis=1))
Output
min() : 2
min(axis=0) : [6 2 2]
min(axis=1) : [2 2 2]
x = np.zeros(3,dtype=int)
print(my_data.min(axis=0,out=x))
print(x)
Output
[6 2 2]
[6 2 2]
Without using axis
y = np.array(1)
print(my_data.min(out=y))
print(y)
Output
2
2
print("min(keepdims=True) : ", my_data.min(keepdims=True))
print("min(keepdims=False) : ", my_data.min(keepdims=False))
Output
min(keepdims=True) : [[2]]
min(keepdims=False) : 2
By using where we can say which elements to use and which elements not to use ( by setting True or False ) . While using where we have to give initial value.
print(my_data.min(where=[True, False,True],initial=2))
Output
2
Using axis with where
print(my_data.min(where=[True, False,True],initial=2))
print(my_data.min(axis=1,where=[True, False,True],initial=1))
print(my_data.min(axis=1,where=[True, False,True],initial=10))
print(my_data.min(axis=0,where=[True, False,True],initial=3))
Output
2
[1 1 1]
[2 2 6]
[3 3 2]
print(my_data.min()) # 2
print(my_data.min(initial=1)) # 1
print(my_data.min(initial=12)) # 2
Output
2
1
2
Check the where option above. The value assigned to intial value is given as output where it is less than the actual output.
print(my_data.min(axis=0)) # [6 2 2]
print(my_data.min(axis=0,initial=12)) # [6 2 2]
print(my_data.min(axis=0,initial=1)) # [1 1 1]
Output
[6 2 2]
[6 2 2]
[1 1 1]
Numpy
mean()
sum()
max()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.