Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

NumPy - Min



What is Min?

In mathematics, the "min" (minimum) refers to the smallest value in a set of numbers. It identifies the least element, providing a measure of the lowest point in a data set.

For example, in the set {3, 1, 4, 2}, the minimum is 1. The minimum is useful for understanding the lower bound of a data set.

The NumPy min() Function

The min() function in NumPy returns the smallest value in an array. It can be applied to the entire array or along a specified axis to find the minimum value in each row or column.

You can also use the amin() function, which is an alias for min() function. Following is the basic syntax of the min() function in NumPy −

numpy.min(a, axis=None, out=None, keepdims=False)

Where,

  • a: The input array or dataset from which the minimum value is to be found.
  • axis: Specifies the axis along which the minimum value is computed. If None (default), the minimum value is computed over the entire array.
  • out: This allows you to specify a location where the result will be stored. If None (default), the result is returned as a new array.
  • keepdims: If True, the reduced dimensions are retained in the result, making it easier for broadcasting. If False (default), the result is squeezed.

Understanding the Min Calculation

The calculation of the minimum value in a dataset is very easy. The function scans through all the elements in the array and identifies the smallest value. This process can be applied to arrays of any shape or size.

Example

Let us understand this concept with an example −

import numpy as np

data = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5])

# Calculating the minimum value
min_value = np.min(data)

print("Minimum value:", min_value)

Following is the output obtained −

Minimum value: 1

Computing Min along Different Axes

In NumPy, the axis parameter allows you to compute the minimum value along specific axes of a multi-dimensional array. The axis parameter refers to the direction along which the minimum value should be calculated. For example, in a 2D array −

  • axis=0: Calculate the minimum value along the columns (vertical axis).
  • axis=1: Calculate the minimum value along the rows (horizontal axis).

Example

In the following example, we are computing the minimum value along both axes of the 2D array −

import numpy as np

# Create a 2D array
data_2d = np.array([[1, 3, 5], [2, 4, 6], [7, 8, 9]])

# Calculate the minimum value along axis 0 (columns)
min_axis_0 = np.min(data_2d, axis=0)

# Calculate the minimum value along axis 1 (rows)
min_axis_1 = np.min(data_2d, axis=1)

print("Minimum value along axis 0:", min_axis_0)
print("Minimum value along axis 1:", min_axis_1)

In the output below, the minimum value along axis 0 is computed by finding the smallest element in each column. The minimum value along axis 1 is calculated by finding the smallest element in each row −

Minimum value along axis 0: [1 3 5]
Minimum value along axis 1: [1 2 7]

Min for Higher-Dimensional Arrays

The numpy.min() function also works for arrays with more than two dimensions. You can specify the axis along which to calculate the minimum value, and the function will return the minimum value for that axis while retaining the other dimensions. If no axis is specified, the minimum value is calculated over the entire array.

Example

Following is an example to compute minimum value of a 3D array −

import numpy as np

# Create a 3D array
data_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])

# Minimum value along axis 0
min_3d_axis_0 = np.min(data_3d, axis=0)

# Minimum value along axis 1
min_3d_axis_1 = np.min(data_3d, axis=1)

# Minimum value along axis 2
min_3d_axis_2 = np.min(data_3d, axis=2)

print("Minimum value along axis 0:", min_3d_axis_0)
print("Minimum value along axis 1:", min_3d_axis_1)
print("Minimum value along axis 2:", min_3d_axis_2)

In this case, the minimum value is calculated along each of the axes (0, 1, and 2) for the 3D array. The function returns the minimum values for each of the specified axes while preserving the other dimensions −

Minimum value along axis 0: [[1 2]
 [3 4]]
Minimum value along axis 1: [[1 2]
 [5 6]]
Minimum value along axis 2: [[1 3]
 [5 7]]

Handling NaN (Not a Number) Values

Sometimes, arrays may contain NaN (Not a Number) values, which can interfere with the calculation of the minimum value. To handle NaN values, NumPy provides an option to ignore them during the min calculation.

You can use the numpy.nanmin() function, which computes the minimum value while ignoring NaN values.

Example

In this example, we are handling NaN values while computing minimum value in NumPy −

import numpy as np

# Create an array with NaN values
data_with_nan = np.array([1, 3, np.nan, 5, 7])

# Calculate the minimum value while ignoring NaN values
min_without_nan = np.nanmin(data_with_nan)

print("Minimum value without NaN:", min_without_nan)

Following is the output obtained −

Minimum value without NaN: 1.0

Using the "Out" Parameter

The out parameter in the numpy.min() function allows you to store the result of the minimum value computation in a pre-allocated array.

This can be useful for memory management and efficiency when working with large datasets. The result is stored in the array specified by the out parameter, which must have the same shape as the expected output.

Example

In this example, the minimum value of the array data is calculated and stored in the pre-allocated array out_array, which is then printed to show the result −

import numpy as np

# Create an array
data = np.array([5, 2, 9, 1, 5, 6])

# Create an output array
out_array = np.empty((), dtype=np.int32)

# Calculate the minimum value and store it in out_array
np.min(data, out=out_array)

print("Output array:", out_array)

Following is the output obtained −

Output array: 1
Advertisements