This blog will discuss the syntax, logic, and use case of the Python NumPy min function. Numpy is a python module used to work with arrays of different dimensions, linear algebra, Fourier transforms, matrices, and other scientific-computing-related topics. We use NumPy for working with arrays rather than built-in python lists because it is way faster and works on the “locality of reference.”
Two arrays are compared, and a new array containing the element-wise minima is returned. If one of the two elements is null, then the initial element is returned as a whole. If both of them are null then the first one is returned.
What is the python numpy minimum()?
The Python NumPy min function helps to calculate the minimum element from the specified arrays. It will take two arrays with equal elements. Then, it will store the minimum element in a particular index in a new minimum array.
import numpy array = numpy.array([55, 33, 77, 11]) array2 = numpy.array([99, 11, 33, 88]) arrayMinimum = numpy.minimum(array, array2) print(arrayMinimum)
The parameters are taken by this function:
The general form of syntax with only compulsory parameters is numpy.minimum(array1, array2)
The python numpy minimum() function of NumPy takes the following parameters. Some of these parameters are compulsory, whereas some are not. The compulsory parameters are:
- array1: This is the first input array, a compulsory parameter required for using this function.
- array2: This is the second input array for which we need to find the minimum elements with respect to the first one.
Description of all the optional as well as compulsory parameters:
|dtype||represents the data type of the array|
|where||to calculate a universal function|
|order||to control the memory layout of the output function|
|casting||to control the type of datacasting to order|
|out||represents where the output is to be stored|
|subok||boolean for subclasses|
Return Type and Value
The python numpy min or minimum function will return the minimum of array1 and array2 but element-wise. The return value is scalar only if both the input arrays are scalar.
1. Both input value is scalar
import numpy as np #two scalars are taken a = 2 b = 3 #the return type is also integer print(np.minimum(a, b))
2. Use case with the NaN input value
import numpy as np #input arrays with nan as elements qarr = np.array([np.nan, 55, 88, np.nan]) arr1 = np.array([np.nan, 90, 22, 2]) #new minimum array calculated element-wise min_array = np.minimum(qarr,arr1) print(min_array)
3. Both input value is one dimensional (1D) array
import numpy as np #declaring an array using the array method of numpy a = np.array([1, 2, 3, 4]) b = np.array([0, 2, 1, 7]) #returning minimum value element-wise from minimum method print(np.minimum(a, b))
4. Both input arrays are two-dimensional (2D)
import numpy as np array1 = np.array([[1, 22, 31, 8], [92, 41, 19, 5]]) array2 = np.array([[np.nan, 90, 30, 19], [101, 39, np.nan, 10]]) print(np.minimum(array1, array2))
Thanks for reading this blog till the end.
If you want to learn more about Complete Python Roadmap for Beginners in 2022, visit this link.