NumPy amin()

Summary: in this tutorial, you will learn how to use the numpy amin() function to find the minimum element in an array.

Introduction to the NumPy amin() function #

The amin() function returns the minimum element of an array or minimum element along an axis. Here’s the syntax of the amin() function:

numpy.amin(a, axis=None, out=None, keepdims=<no value>, initial=<no value>, where=<no value>)

The amin() function is equivalent to the min() method of the ndarray object:

ndarray.min(axis=None, out=None, keepdims=False, initial=<no value>, where=True)

NumPy amin() function examples #

Let’s take some examples of using the amin() function.

1) Using numpy amin() function on 1-D array example #

The following example uses the numpy amin() function to find the minimum value in a 1-D array:

import numpy as np


a = np.array([1, 2, 3])
min = np.amin(a)
print(min)

Output:

1

How it works.

First, create a new array that has three numbers 1, 2, and 3:

a = np.array([1, 2, 3])

Second, find the minimum number using the amin() function:

min = np.amin(a)

Third, display the result:

print(min)

2) Using numpy amin() function on multidimensional array examples #

The following example uses the amin() funciton to find the minimum number in a 2-D array:

import numpy as np

a = np.array([
    [1, 2],
    [3, 4]]
)
min = np.amin(a)
print(min)

Output:

1

If you want to find the minimum value on each axis, you can use the axis argument. For example, the following uses the amin() function to find the minimum value on axis 0:

import numpy as np

a = np.array([
    [1, 2],
    [3, 4]]
)
min = np.amin(a, axis=0)
print(min)
Image

Output:

[1 2]

Similarly, you can use the amin() function to find the minimum value on axis 1:

import numpy as np

a = np.array([
    [1, 2],
    [3, 4]]
)
min = np.amin(a, axis=1)
print(min)
Image

Output:

[1 3]

Summary #

  • Use the numpy amin() function to find the minimum element in an array or minimum element along an axis.

Was this helpful?