NumPy amin()

Created with Sketch.

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>)

Code language: Python (python)

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)

Code language: Python (python)

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)

Code language: Python (python)

Output:

1

Code language: Python (python)

How it works.

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

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

Code language: Python (python)

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

min = np.amin(a)

Code language: Python (python)

Third, display the result:

print(min)

Code language: Python (python)

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)

Code language: Python (python)

Output:

1

Code language: Python (python)

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)

Code language: Python (python)

Output:

[1 2]

Code language: Python (python)

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)

Code language: Python (python)

Output:

[1 3]

Code language: Python (python)

Summary

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

Leave a Reply

Your email address will not be published. Required fields are marked *