NumPy amin – Return the Minimum of Array Elements using Numpy

NumPy Amin Cover Image

Hello and welcome to this tutorial on Numpy amin. In this tutorial, we will be learning about the NumPy amin() method and also seeing a lot of examples regarding the same. So let us begin!

Also read: Numpy.subtract(): How to Use Subtract Numbers with NumPy in Python?


What is NumPy amin?

The amin method in NumPy is a function that returns the minimum of the array elements. It can be the minimum of all the array elements, the minimum of the array elements along the rows, or the minimum of the array elements along the columns.

We will see the examples for each of these in the upcoming section of this tutorial.


Syntax of NumPy amin

numpy.amin(a, axis=None, out=None)
ParameterDescriptionRequired/Optional
a (array_like)Input data.Required
axisAxis along which the minimum of the array is to be calculated. It can be axis=0 or axis=1 or axis=None which implies that the minimum of the flattened array is to be returned.Optional
outAn alternative output array in which to place the result. It must have the same shape as the expected output.Optional

Returns: The minimum element in a. If axis=None, then the output is a scalar, otherwise, the output is an array.


Examples of numpy.amin()

Let’s get right into the different examples of using numpy.amin() function.

Using numpy.amin() when the array is 1-dimensional

import numpy as np

a = [10, 3, 25]

ans = np.amin(a)
print("a =", a)
print("Minimum of a =", ans)

Output:

a = [10, 3, 25]
Minimum of a = 3

Comparing all the elements in the given array, the minimum of 10, 3 and 25 is 3. Hence, 3 are returned.


Using numpy.amin() when the array contains negative numbers

import numpy as np

a = [[-8, 6], [-5, -12]]

ans = np.amin(a)
print("a =", a)
print("Minimum of a =", ans)

Output:

a = [[-8, 6], [-5, -12]]
Minimum of a = -12

Comparing all the values in the array, -12 is the minimum element here.


Using numpy.amin() when the array contains NaN values

In Python, NaN stands for Not a Number.

import numpy as np

a = [26, np.nan, 8, np.nan, -4]

ans = np.amin(a)
print("a =", a)
print("Minimum of a =", ans)

Output:

a = [26, nan, 8, nan, -4]
Minimum of a = nan

If the input contains NaNs, then the NumPy amin() method always returns nan as output, irrespective of the other elements present in the input array.


Using numpy.amin() when the array is 2-dimensional

import numpy as np

a = [[16, 3], [48, 66]]

ans = np.amin(a)
print("a =", a)
print("Minimum of a =", ans)

Output:

a = [[16, 3], [48, 66]]
Minimum of a = 3

In the case of a 2-dimensional array, when no axis is mentioned, the array is first flattened row-wise and then its minimum is calculated.
In the above example, the flattened array will be [16, 3, 48, 66] and the minimum element in it is 3, hence it is returned by the amin() method.


Using numpy.amin() to find the minimum along a given axis

axis = 0

import numpy as np

a = [[16, 3], [48, 66]]
# minimum along axis=0
ans = np.amin(a, axis=0)
print("a =", a)
print("Minimum of a =", ans)

Output:

a = [[16, 3], [48, 66]]
Minimum of a = [16  3]

Here, the elements are compared column-wise and their minimum is stored in the output.

ans[0] = minimum(a[0][0], a[1][0]) = minimum(16, 48) = 16
ans[1] = minimum(a[0][1], a[1][1]) = minimum(3, 66) = 3

axis= 1

import numpy as np

a = [[16, 3], [48, 66]]
# minimum along axis=1
ans = np.amin(a, axis=1)
print("a =", a)
print("Minimum of a =", ans)

Output:

a = [[16, 3], [48, 66]]
Minimum of a = [ 3 48]

Here, the elements are compared row-wise and their minimum is stored in the output.

ans[0] = minimum(a[0][0], a[0][1]) = minimum(16, 3) = 3
ans[1] = minimum(a[1][0], a[1][1]) = minimum(48, 66) = 48

Conclusion

That’s all! In this tutorial, we learned about the Numpy amin method and practiced different types of examples using the same.  If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference