It is easy to pick an odd one out! But when it comes to picking the smallest one out, especially when there is an abundance of data from which the picking is to be done, there comes the hassle! To relieve us of this burden of effort & time that a human mind shall take to do it, we find solace in the computational capabilities of programming languages.
In this article, we will look into the sequence of steps involved in finding the minimum out of the given elements in an array using the minimum( ) function from the numpy library in Python. Let us get started with understanding its syntax.
Syntax of minimum( ) function
Following are the basic constructs comprising both mandatory and optional elements that ought to be put together for the effective functioning of the minimum( ) function from the numpy library.
numpy.minimum(x1, x2, out=None, *, where=True, dtype=None)
- x1, x2 – input arrays holding the elements within which the minimum is to be found
- out – an optional construct set to none by default, but could be used to store the results in the desired array which is of the same length as the output
- * – kwargs or keyword argument which is an optional construct used to pass keyword variable length of argument to a function
- where –an optional construct which is used to calculate the universal function (ufunc) at the given position when set to True (default setting) or not calculate when set to False
- dtype – an optional construct used to specify the data type which is being used
Using minimum( ) on One Dimensional Arrays
Let us kick things off by importing the numpy library within Python by using the following code.
import numpy as np
Following would be the one-dimensional arrays for which the minimum elements within are to be determined.
ar1 = np.array([[1.2, 3.4, 6.7, 8.9]], dtype = int) ar2 = np.array([[2.1, 4.3, 5.7, 6.9]], dtype = int)
It could be noticed above that the data type being considered is int so one can very well expect the output to be stripped off the decimal numbers. Now it is time to use the minimum( ) function!
np.minimum(ar1, ar2, dtype = int)
Using Numpy minimum( ) on N-Dimensional Arrays
Let us now extend our ask by using arrays of multiple dimensions such as those given below to return their minimum elements using the minimum( ) function.
ar1 = np.array([[1.2, 3.4, 6.7, 8.9], [9.8, 7.6, 5.4, 3.2]], dtype = float) ar2 = np.array([[2.1, 4.3, 5.7, 6.9], [9.7, 8.6, 4.5, 1.2]], dtype = float) np.minimum(ar1, ar2, dtype = float)
Similar to the output array of the previous section, here too each position of the output array which is of the same dimensions as the input arrays are replaced with the minimum of the values.
Using where in Minimum( ) function
One can also selectively find the minimum of the given array elements confined to only a particular position within the array by exercising the where option in the minimum( ) function as demonstrated below.
ar1 = np.array([[1.2, 3.4, 6.7, 8.9], [9.8, 7.6, 5.4, 3.2]], dtype = float) ar2 = np.array([[2.1, 4.3, 5.7, 6.9], [9.7, 8.6, 4.5, 1.2]], dtype = float) np.minimum(ar1, ar2, where = [False, True, True, True])
The above code restricts the minimum( ) function to only return the minimum values of the input arrays compared in all the positions except for the first.
Now that we have reached the end of this article, hope it has elaborated on how to find the minimum of array elements using the minimum( ) function from the numpy library. Here’s another article that explains how to divide elements in an array using numpy in Python. There are numerous other enjoyable & equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Whilst you enjoy those, hasta luego!