NumPy amin()

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

Example

import numpy as np

array1 = np.array([5, 2, 8, 1, 9])

# find minimum value from array1 max_value = np.amin(array1)
print(max_value) # Output: 1

amin() Syntax

The syntax of amin() is:

numpy.amin(a, axis = None, keepdims = False)

amin() Arguments

The amin() function takes following arguments:

  • a - the input array
  • axis (optional) - the axis along which the minimum value is computed
  • keepdims (optional) - whether to preserve the input array's dimension (bool)

amin() Return Value

The amin() function returns the minimum element from an array or along a specified axis.


Example 1: amin() With 2-D Array

The axis argument defines how we can find the minimum element in a 2-D array.

  • If axis = None, the array is flattened and the minimum value of the flattened array is returned.
  • If axis = 0, the minimum value is calculated column-wise.
  • If axis = 1, the minimum value is calculated row-wise.
import numpy as np

array = np.array([[10, 17, 25], 
                              [15, 11, 22]])
                  
# calculate the minimum value of the flattened array result1 = np.amin(array)
print('The minimum value of the flattened array:', result1)
# calculate the column-wise minimum values result2 = np.amin(array, axis=0)
print('Column-wise minimum values (axis 0):', result2)
# calculate the row-wise minimum values result3 = np.amin(array, axis=1)
print('Row-wise minimum values (axis 1):', result3)

Output

The minimum value of the flattened array: 10
Column-wise minimum values (axis 0): [10 11 22]
Row-wise minimum values (axis 1): [10 11]

Here,

  1. np.amin(array) calculates the minimum value of the flattened array. It returns the largest element in the entire array.
  2. np.amin(array, axis=0) calculates the column-wise minimum values. It returns an array containing the minimum value for each column.
  3. np.amin(array, axis=1) calculates the row-wise minimum values. It returns an array containing the minimum value for each row

Example 2: amin() With keepdims

When keepdims = True, the dimensions of the resulting array matches the dimension of an input array.

import numpy as np

array1 = np.array([[10, 17, 25], 
                                [15, 11, 22]])

print('Dimensions of original array:', array1.ndim)

result = np.amin(array1, axis=1)

print('\nWithout keepdims:')
print(result)
print('Dimensions of array:', result.ndim) 

# set keepdims to True to retain the dimension of the input array result = np.amin(array1, axis=1, keepdims=True)
print('\nWith keepdims:') print(result) print('Dimensions of array:', result.ndim)

Output

Dimensions of original array: 2

Without keepdims:
[10 11]
Dimensions of array: 1

With keepdims:
[[10]
 [11]]
Dimensions of array: 2

Without keepdims, the result is simply a one-dimensional array containing the minimum values along the specified axis.

With keepdims, the resulting array has the same number of dimensions as the input array.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges