NumPy min() function in Python [3 use cases]

Do you want to find the minimum value inside an array? In this Python tutorial, I will explain what is the NumPy min() function in Python, including its syntax, parameters, return value, and examples of its use. Also, what is the np.min vs np.minimum in Python?

To find the minimum value in an array, the NumPy min() function in Python is used. It calculates the minimum across the specified axis of the array, and with keepdims=True, it preserves the dimensionality of the result. In contrast, np.minimum() performs an element-wise minimum comparison between two arrays, whereas np.min() focuses on finding the minimum within a single array or along its specified axis.

NumPy min() function in Python

The NumPy min() function in Python, is used to find the minimum value in an array. It can operate across a specified axis for multi-dimensional arrays, returning the minimum value along that axis.

np.min() function in Python syntax

The basic syntax of numpy.min() is as follows:

numpy.min(arr, axis=None, out=None, keepdims=<no value>)

NumPy min() function in Python parameters required

arrThe input array in Python from which the minimum value needs to be found.
axisThe axis along which to find the minimum. If not specified, the minimum of the entire array is returned.
outAn alternative output array in which to place the result.
keepdimsIf this is set to True, the reduced axes are left in the result as dimensions with size one.
List the parameters required in the np.min() function in Python.

np.min in Python return value

The NumPy min() function in Python returns the minimum of an array or minimum along the specified axis. The returned value is a NumPy array with the minimum values.

np.min function use cases

Let’s see some examples related to the working of the NumPy min() function in Python.

1. NumPy min of two arrays

To find the minimum of the values stored in an array in Python, we can pass the array to the NumPy min() function in Python

import numpy as np

temperatures = np.array([58, 60, 57, 59, 61])
print("Minimum annual average temperature:", np.min(temperatures))

Output:

Minimum annual average temperature: 57

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

NumPy min() function in Python

2. np.min function on a 2D array in Python

We can find the minimum value within a 2D array in Python, whether it can be within a row, within a column, or the whole array.

To find the minimum value along the rows and columns we need to use the axis parameter:

  • axis=1: which will find the minimum value along the columns.
  • axis=0: which will find the minimum value along the rows.

Here’s the full code that will contain all the ways to use the NumPy min() function in Python:

import numpy as np

temperatures = np.array([
    [58, 60, 59, 61, 62, 60, 59],  # New York
    [75, 76, 74, 73, 72, 74, 75],  # Los Angeles
    [50, 52, 51, 49, 48, 47, 50]   # Chicago
])

# Finding the minimum temperature for each city across the week
min_temperatures = np.min(temperatures, axis=1)
print("Minimum temperatures for each city: New York, Los Angeles, Chicago")
print(min_temperatures)

# Finding the minimum temperature experienced within the cities
min_temperatures = np.min(temperatures, axis=0)
print("Minimum temperatures within the cities")
print(min_temperatures)

# Finding the minimum temperature ever experienced
min_temperatures = np.min(temperatures)
print("Minimum temperatures among all cities")
print(min_temperatures)

Output:

Minimum temperatures for each city: New York, Los Angeles, Chicago
[58 72 47]
Minimum temperatures within the cities
[50 52 51 49 48 47 50]
Minimum temperatures among all cities
47

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

numpy min function in Python

3. NumPy min of array in Python

Here, we will use the keepdims parameter with the NumPy min() function in Python.

import numpy as np

rainfall = np.array([[3.2, 2.5, 4.1, 3.3, 5.0, 2.8], [4.2, 3.6, 3.9, 4.5, 4.8, 3.1]])
print("Minimum monthly rainfall in each city:\n", np.min(rainfall, axis=1, keepdims=True))

Output:

Minimum monthly rainfall in each city:
 [[2.5]
 [3.1]]

After executing the code in Pycharm, one can see the output in the below screenshot.

numpy min float in Python 2D array

np.min vs np.minimum in Python

np.min()Finds the minimum value in an array in Python or along a specified axis.
mp.minimum()Performs element-wise comparison between two arrays in Python and returns the element-wise minima.

Examples: Here is the full code which shows the difference between np.min() and np.minimum() functions in Python.

import numpy as np
Temperatures = np.array(
    [[32, 28, 35],  # Temperatures in New York, Chicago, Boston
    [75, 80, 70]]  # Temperatures in Miami, Houston, New Orleans
)
minimum_temp = np.min(Temperatures)
print("The use of the np.min() function:\n", minimum_temp)

Boston_temperatures = np.array([32, 28, 35])  # Temperatures in 3 days
Chicago_temperatures = np.array([34, 27, 25])  # Temperatures in 3 days

minimum_temp = np.minimum(Boston_temperatures, Chicago_temperatures)
print("The use of the np.minimum() function:\n", minimum_temp)

Output:

The use of the np.min() function:
 28
The use of the np.minimum() function:
 [32 27 25]

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

numpy min of array in Python

Conclusion

Here, we have learned how we can use the NumPy min() function in Python with its syntax, parameters required, return value, and some illustrative examples like np.min() function in an array, along axes, using keepdims parameter, etc. We have also seen the basic difference between the np.min() and np.minimum() in Python.

You may also like to read: