Python NumPy minimum()
function is used to compare the two arrays, element wise and returns the minimum values in a new array that contains element-wise minimum. While comparing, one of the elements of two arrays is a NaN, then that element is returned. If both elements of two arrays are NaNs then the first element is returned.
In this article, I will explain how to find the minimum value of Numpy arrays using numpy.minimum(
) functions with examples.
1. Quick Examples of Python NumPy minimum() Function
If you are in a hurry, below are some quick examples of how to use the NumPy minimum() function.
# Below are the quick examples
# Example :1 Get minimum value
arr = 34
arr1 = 65
arr2 = np.minimum(arr, arr1)
# Example 2: Get minimum value of 1-D arrays
arr = np.array([26,38,68,79])
arr1 = np.array([43,28,55,84])
arr2 = np.minimum(arr,arr1)
# Example 3: Use numpy.minimum() Function & NaN
arr = np.array([np.nan, 8, 17, np.nan, 48])
arr1 = np.array([np.nan, 24, np.nan, np.nan, 35])
arr2 = np.minimum(arr,arr1)
# Example 4: Get minimum value of two-D arrays
arr = np.array([[26,38,68,79],[34,47,np.nan,20]])
arr1 = np.array([[43,28,55,84],[np.nan,32,43,np.nan]])
arr2 = np.minimum(arr,arr1)
2. Syntax of minimum()
Following is the syntax of the numpy.minimum()
function.
# Syntax of numpy.minimum()
numpy.minimum(arr1, arr2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, ufunc 'minimum')
2.1 Parameters of minimum()
arr,arr1
– Input arraysout
– ndarray, optional] A location in which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.where
– This condition is broadcast over the input, at locations where the condition is True, the out array will be set to the ufunc result.**kwargs
– allows you to pass keyword variable length of argument to a function. It is used when we want to handle a named argument in a function.where
– [array_like, optional]True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.
2.2 Return Value of minimum()
It returns the minimum of arr1 and arr2 arrays, element-wise. This returns a scalar if both arr1 and arr2 are scalars.
3. Usage of Numpy minimum()
minimum()
function is used to get a new array that contains element-wise minimum values of two arrays. It compares two arrays and returns a new array containing the minimum values. If one of the elements being compared is NaN
(Not a Number), NaN
is returned. If both elements being compared are NaN
(Not a Number), then NAN
is returned.
3.1 Get Minimum Value of Two Scalars
If we use numpy.minimum()
function to compare two scalar values, it will return the minimum scalar value of two scalars. For example,
import numpy as np
arr = 34
arr1 = 65
# Get minimum value
arr2 = np.minimum(arr, arr1)
print (arr2)
# Output :
# 34
3.2 Get Minimum Values of 1-D Array
The following example demonstrates how to get the minimum values of 1-D NumPy arrays using minimum()
. Let’s create 1-D arrays using numpy.array() and pass these two arrays as input to the minimum() function. For example,
import numpy as np
# Create 1-D arrays
arr = np.array([26,38,68,79])
arr1 = np.array([43,28,55,84])
# Get minimum value of 1-D arrays
arr2 = np.minimum(arr,arr1)
print(arr2)
# Output :
# [26 28 55 79]
4. Using NaN in NumPy Arrays
If there is a NaN in the given NumPy array then minimum()
will return NaN as the minimum value. If both elements of two arrays are NaNs then it returns the first element.
arr = np.array([np.nan, 8, 17, np.nan, 48])
arr1 = np.array([np.nan, 24, np.nan, np.nan, 35])
# Use numpy.minimum() Function & NaN
arr2 = np.minimum(arr,arr1)
print(arr2)
# Output:
# [nan 8. nan nan 35.]
5. Get Minimum Value of 2-D Arrays
Finally, let’s get the minimum values element-wise by comparing two 2-D NumPy arrays.
# Create 2-D arrays
arr = np.array([[26,38,68,79],[34,47,np.nan,20]])
arr1 = np.array([[43,28,55,84],[np.nan,32,43,np.nan]])
# Get minimum value of two-D arrays
arr2 = np.minimum(arr,arr1)
print(arr2)
# Output :
# [[26. 28. 55. 79.]
# [nan 32. nan nan]]
6. Conclusion
In this article, I have explained how to get the minimum values of Numpy arrays using minimum()
function with examples. While comparing, one of the elements of two arrays is a NaN, then that element is returned. If both elements of two arrays are NaNs then the first element is returned.
Happy Learning!!
Related Articles
- How to get square values of an array ?
- How to create NumPy one’s array ?
- numpy.delete() Function
- numpy.divide() Function
- How to get maximum of NumPy array ?
- How to Check NumPy Array Equal?
- Python NumPy Reverse Array
- Python NumPy Split Array – Using split() Function
- How to Calculate Maximum of Array in NumPy