numpy.amin() function is an alias for numpy.min() function.

NumPy amin()

The numpy.amin() function returns the minimum value in an array or the minimum value along a specified axis.

Syntax

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

Parameters

ParameterTypeDescription
aarray_likeInput array from which the minimum value is computed.
axisNone, int, or tuple of ints, optionalAxis or axes along which to find the minimum. If None, the minimum of the flattened array is returned.
outndarray, optionalAlternative output array to store the result. Must have the same shape as expected output.
keepdimsbool, optionalIf True, the reduced dimensions are kept as size one, allowing proper broadcasting.
initialscalar, optionalSpecifies an initial minimum value for empty arrays.
wherearray_like of bool, optionalSpecifies elements to include in the comparison for the minimum value.

Return Value

Returns a scalar or an array containing the minimum values. If axis=None, a scalar value is returned. If axis is specified, an array with the reduced dimension is returned.


Examples

1. Finding the Minimum Value in an Array

This example demonstrates finding the smallest element in an array.

</>
Copy
import numpy as np

# Define an array
arr = np.array([3, 1, 4, 1, 5, 9, 2, 6])

# Find the minimum value
min_value = np.amin(arr)

# Print the result
print("Minimum value in the array:", min_value)

Output:

Minimum value in the array: 1

2. Finding the Minimum Along an Axis

We can compute the minimum values along a specific axis in a multi-dimensional array.

</>
Copy
import numpy as np

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

# Minimum along axis 0 (column-wise)
min_axis0 = np.amin(arr, axis=0)

# Minimum along axis 1 (row-wise)
min_axis1 = np.amin(arr, axis=1)

# Print results
print("Minimum values along axis 0 (columns):", min_axis0)
print("Minimum values along axis 1 (rows):", min_axis1)

Output:

Minimum values along axis 0 (columns): [1 6 2]
Minimum values along axis 1 (rows): [2 1 4]

3. Using the out Parameter

Using an output array to store the minimum values instead of creating a new array.

</>
Copy
import numpy as np

# Define an array
arr = np.array([4, 2, 7, 1, 8])

# Create an output array with the same shape
output_array = np.empty(shape=())

# Compute minimum and store the result in output_array
np.amin(arr, out=output_array)

# Print the result
print("Minimum value using out parameter:", output_array)

Output:

Minimum value using out parameter: 1.0

4. Using the where Parameter

Using a condition to find the minimum only in specific elements.

</>
Copy
import numpy as np

# Define an array
arr = np.array([5, 1, 8, 3, 2])

# Define a mask (consider only selected elements)
mask = np.array([True, False, True, True, False])

# Compute minimum value considering only masked elements
result = np.amin(arr, where=mask, initial=100)

# Print the result
print("Minimum value where mask is True:", result)

Output:

Minimum value where mask is True: 3

The minimum value is computed only for the elements where mask=True. The ignored elements do not affect the result.