Last modified: Oct 21, 2024 By Alexander Williams

Understanding Python numpy.min()

The numpy.min() function in Python is used to find the minimum value within an array. It is a simple yet essential method for data analysis, especially when working with numerical datasets.

Prerequisites

Before using numpy.min(), ensure that you have NumPy installed in your Python environment. For installation help, see our guide on How to Install NumPy in Python.

Syntax of numpy.min()

The syntax for numpy.min() is straightforward and user-friendly:


import numpy as np
np.min(a, axis=None)

Here, a is the input array, and the optional axis parameter determines the axis along which the minimum value is computed.

Examples of numpy.min()

Example 1: Finding the Minimum Value of an Array

Let's find the minimum value in a 1D array:


import numpy as np

arr = np.array([3, 1, 4, 1, 5, 9])
min_value = np.min(arr)
print(min_value)


1

The minimum value in the array [3, 1, 4, 1, 5, 9] is 1.

Example 2: Finding the Minimum Value Along an Axis

When working with multi-dimensional arrays, you can use the axis parameter to find the minimum value along a specific axis:


import numpy as np

arr = np.array([[3, 2, 5], [1, 4, 7], [8, 6, 0]])
min_col = np.min(arr, axis=0)
min_row = np.min(arr, axis=1)

print("Minimum along columns:", min_col)
print("Minimum along rows:", min_row)


Minimum along columns: [1 2 0]
Minimum along rows: [2 1 0]

In this example, numpy.min() finds the minimum values along the columns and rows of a 2D array.

Example 3: Using numpy.min() with numpy.arange()

Combine numpy.min() with numpy.arange() to analyze ranges of numbers:


import numpy as np

arr = np.arange(1, 10)
min_value = np.min(arr)
print(min_value)


1

This example creates an array using numpy.arange() and finds the minimum value in the range.

Applications of numpy.min()

The numpy.min() function is commonly used in data preprocessing and analysis. It helps identify the smallest data points in datasets, which is particularly useful in fields like data science and machine learning.

It can be used alongside functions like numpy.reshape() and numpy.array() for more complex data manipulations.

For more details on data analysis in Python, you can also check out this resource on Real Python's NumPy tutorial.

Conclusion

The numpy.min() function is a valuable tool for finding the minimum value in arrays. Whether working with 1D or multi-dimensional arrays, it offers a simple way to identify the smallest elements in your data.

To explore similar topics, read our articles on Understanding Python numpy.linspace() and Understanding Python numpy.zeros().