NumPy amin() Function



The NumPy amin() function returns the minimum value of an array along a specified axis. If no axis is specified, it computes the minimum value across all elements in the array. Additionally, conditions can be applied to obtain the minimum value based on specific criteria.

Syntax

Following is the syntax of the NumPy amin() function −

numpy.amin(a, axis=None, out=None, keepdims=False, initial=None, where=True)  

Parameters

Following are the parameters of the NumPy amin() function −

  • a: Input array. The array can be of any shape or data type.
  • axis (optional): Axis along which to compute the minimum. If None, the minimum is computed over the flattened array.
  • out (optional): Alternate output array to place the result. It must have the same shape as the expected output.
  • keepdims (optional): If True, the reduced dimensions are retained as dimensions of size one in the output. Default is False.
  • initial (optional): Initial value to start the comparison. If not provided, the default is the maximum possible value for the data type.
  • where (optional): A Boolean array. If True, include the corresponding element in the computation; otherwise, ignore it.

Return Values

This function returns a scalar value or a NumPy array containing the minimum values along the specified axis.

Example

Following is a basic example of finding the minimum value in an array using the NumPy amin() function −

import numpy as np  
# input array  
array = np.array([3, 5, 1, 7, 9])  
# finding the minimum value  
min_value = np.amin(array)  
print("Minimum Value:", min_value)  

Output

Following is the output of the above code −

Minimum Value: 1  

Example: Minimum Along an Axis

The amin() function can find the minimum values along a specified axis of a multi-dimensional array. In the following example, we compute the minimum values along rows and columns −

import numpy as np  
# 2D input array  
array = np.array([[3, 7, 5], [8, 4, 2], [6, 1, 9]])  
# minimum along rows (axis=1)  
min_along_rows = np.amin(array, axis=1)  
print("Minimum along rows:", min_along_rows)  
# minimum along columns (axis=0)  
min_along_columns = np.amin(array, axis=0)  
print("Minimum along columns:", min_along_columns)  

Output

Following is the output of the above code −

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

Example: Minimum with keepdims

The keepdims parameter retains the reduced dimension as a size-one dimension in the output. In this example, we demonstrate its use −

import numpy as np  
# 2D input array  
array = np.array([[3, 7, 5], [8, 4, 2], [6, 1, 9]])  
# minimum along rows with keepdims=True  
min_with_keepdims = np.amin(array, axis=1, keepdims=True)  
print("Minimum with keepdims:\n", min_with_keepdims)  

Output

Following is the output of the above code −

Minimum with keepdims:  
[[3]  
 [2]  
 [1]]  

Example: Minimum with 'where' Condition

The where parameter allows computation of the minimum based on a condition. In the following example, we compute the minimum only for selected elements −

import numpy as np  
# input array  
array = np.array([3, 5, 1, 7, 9])  
# where condition (only include values greater than 4)  
min_with_condition = np.amin(array, where=array > 4, initial=10)  
print("Minimum with condition:", min_with_condition)  

Output

Following is the output of the above code −

Minimum with condition: 5  

Example: Graphical Representation of 'amin()'

In the following example, we visualize the minimum value along rows and columns of a 2D array. To achieve this, we need to import the numpy and matplotlib.pyplot modules −

import numpy as np  
import matplotlib.pyplot as plt  

# 2D input array  
array = np.array([[3, 7, 5], [8, 4, 2], [6, 1, 9]])  
# minimum along rows  
min_rows = np.amin(array, axis=1)  
# minimum along columns  
min_columns = np.amin(array, axis=0)  

plt.plot(range(len(min_rows)), min_rows, label="Minimum along rows")  
plt.plot(range(len(min_columns)), min_columns, label="Minimum along columns")  
plt.title("Visualization of amin() Results")  
plt.xlabel("Index")  
plt.ylabel("Minimum Value")  
plt.legend()  
plt.grid()  
plt.show()  

Output

The plot visualizes the minimum values along rows and columns of the array −

amin Visualization
numpy_statistical_functions.htm
Advertisements