# How to Calculate minimum() of Array in NumPy?

• Post author:
• Post category:NumPy / Python

Python NumPy `minimum()` or `min()` function is used to get the minimum value (lowest value) of a given array, or compare the two arrays element-wise and return the minimum values. While comparing, one of the elements of two arrays is a NaN, then that element is returned as NaN. If both elements of two arrays are NaNs then the first element is returned. Use the maximum() function to get the maximum values of the NumPy array.

In this article, I will explain how to get the minimum values of 1-D arrays in element-wise and multi-D arrays along with specified axes using syntax and parameters of `numpy.minimum()` function with examples.

## 1. Quick Examples of Calculate Minimum of Array

If you are in a hurry, below are some quick examples of how to use the NumPy minimum() function.

``````
# Quick examples of calculate minimum of array

# Example :1 Get the minimum value
# Of two scalars
arr = 34
arr1 = 65
min_value = np.minimum(arr, arr1)

# Example :2 Get the minimum value
arr = np.array([14, 8, 12, 20, 17, 15])
min_value = np.minimum(arr)

# Example :3 Get the element-wise minimum
arr = np.array([26,38,68,79])
arr1 = np.array([43,28,55,84])
min_value = np.minimum(arr,arr1)

# Example :4 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])
min_value = np.minimum(arr,arr1)

# Example :5 Get the minimum value of 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]])
max_value = 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()

Following are the parameters of minimum().

• `arr,arr1`– Input arrays to be compared element-wise. They must be broadcastable to the same shape, or one of them should be a scalar.
• `out` – 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 parameter is optional. It is a boolean array that indicates where the result should be calculated. If `True`, the calculation is performed; if `False`, the value is propagated. By default, it is set to `True`.
• `order` – This parameter is optional and specifies the memory layout of the output array. It can be `C` (row-major), `F` (column-major), or `K` (match input layout).
• `dtype` – Data type of the output array. If not specified, the data type is determined from the inputs.
• `**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.

### 2.2 Return Value

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()

NumPy` 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), the result for that pair is also NaN. Similarly, if both elements being compared are `NaN`, the result is `NaN`.

### 3.1 Get Minimum Value of Two Scalars

You can get the minimum value of two scalar variables `arr` and `arr1`. In this case, `arr` has a value of `34`, and `arr1` has a value of `65`. For instance, `minimum(arr, arr1)` returns the minimum value between `arr` and `arr1`, and the result is stored in the variable `min_value`. The `print()` statement then displays the minimum value.

``````
# Import numpy
import numpy as np

# Initialize variables
arr = 34
arr1 = 65

# Get minimum value of two scalars
min_value = np.minimum(arr, arr1)
print ("Minimum value:", min_value)
``````

Yields below output.

### 3.2 Get Minimum Values of 1-D Array

To get the minimum value of a 1-D array in Python, you can use the `numpy.minimum()` function in NumPy. For instance, `np.minimum(arr)` returns the minimum value of the 1-D array `arr`, and the result is stored in the variable `min_value`. The `print()` statement then displays the minimum value.

``````
# Create a 1-D NumPy array
arr = np.array([14, 8, 12, 20, 17, 15])
print("Original array:", arr)

# Get the minimum value
min_value = np.minimum(arr)
print("Minimum value of 1-D array:", min_value)

# Output:
# Original array: [14  8 12 20 17 15]
# Minimum value of 1-D array: 8
``````

## 4. Get a Minimum of Two NumPy Arrays

Alternatively, to get the element-wise minimum of two NumPy arrays, you can use the `numpy.minimum()` function.

In the below example, `np.minimum(arr, arr1)` returns a new array where each element is the minimum of the corresponding elements in `arr` and `arr1`. The resulting array is stored in the variable `arr2`, and the `print()` statement displays the original arrays and the element-wise minimum.

``````
# Create 1-D arrays
arr = np.array([26,38,68,79])
print("First array:\n",arr)
arr1 = np.array([43,28,55,84])
print("Second array:\n",arr1)

# Get the element-wise minimum
min_value = np.minimum(arr,arr1)
print("Element-wise Minimum:\n",min_value)
``````

Yields below output.

## 5. 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.

You can use the `numpy.minimum()` function to perform an element-wise minimum comparison between two NumPy arrays containing NaN values. The `numpy.minimum()` function handles NaN values according to the specified rules.

``````
# Create two NumPy arrays with NaN values
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
min_value = np.minimum(arr,arr1)
print("After the Element-wise minimum of the arrays:\n",min_value)

# Output:
# After the Element-wise minimum of the arrays:
#  [nan  8. nan nan 35.]
``````

In the result:

• The minimum of (NaN, NaN) is NaN.
• The minimum of (NaN, 8) is 8.
• The minimum of (17, NaN) is NaN.
• The minimum of (NaN, NaN) is NaN.
• The minimum of (48, 35) is 35.

So, the resulting array (`min_value`) contains the element-wise minimum values of the corresponding elements in `arr` and `arr1`, and NaN values are handled appropriately.

## 6. Get the Minimum Value of 2-D Arrays

You can also use the `numpy.minimum()` function to perform an element-wise minimum comparison between two 2-D NumPy arrays. The `numpy.minimum()` function handles NaN values according to the specified rules.

So, the resulting array (`min_value`) contains the element-wise minimum values of the corresponding elements in `arr` and `arr1`, and NaN values are handled appropriately.

``````
# 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 2-D arrays
max_value = np.minimum(arr,arr1)
print("Minimum value of the entire array:\n", max_value)

# Output:
# Minimum value of the entire array:
#  [[26. 28. 55. 79.]
#  [nan 32. nan nan]]
``````

How do I calculate the minimum value of a 1-D array in NumPy?

To calculate the minimum value of a 1-D array in NumPy, you can use the `numpy.min()` function. For example, `np.min(arr)` returns the minimum value of the 1-D array `arr`, and the result is stored in the variable `min_value`.

Can I calculate the minimum value along a specific axis for a 2-D array?

You can calculate the minimum value along a specific axis for a 2-D array using the `axis` parameter in the `numpy.min()` function. The `axis` parameter allows you to specify along which axis you want to compute the minimum values.

How does NumPy handle NaN values in minimum calculations?

When using `numpy.min()`, NaN values are handled naturally. If one of the elements being compared is NaN, the other non-NaN element is chosen. If both elements are NaN, the first element is returned.

How can I calculate the element-wise minimum of two arrays, including handling NaN values?

To calculate the element-wise minimum of two arrays, including handling NaN values, you can use the `numpy.minimum()` function. This function performs element-wise minimum comparison between corresponding elements of two arrays and handles NaN values according to specific rules.

Can I calculate the element-wise minimum of two 2-D arrays in NumPy?

You can calculate the element-wise minimum of two 2-D arrays in NumPy using the `numpy.minimum()` function. This function compares corresponding elements of two arrays and returns a new array where each element is the minimum of the corresponding elements in the input arrays.

## Conclusion

In this article, I have explained how to get the minimum values of Numpy arrays using `minimum()` or `min()` 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!!

## References

### Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users. 