np.amin: What is Numpy amin() Function in Python

The amin() method in Numpy returns a minimum of an array or minimum along the axis(if mentioned). In addition, python’s numpy module provides the function to get the minimum value from a Numpy array.

np.amin

The np.amin() is a numpy library method used to get the minimum value from the ndarray. The amin() method takes arr, axis, out, keepdims, initial, and returns the minimum array.

For a single-dimensional array, we can easily find the smallest element, but for the multidimensional array, we can also find the smallest element of each row and column.

Syntax

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

Parameters

The Numpy amin() function takes up to 4 arguments: the following.

  1. arr -> This is the array from which we can find the min value
  2. axis -> This indicates the axis along which we want to find the smallest element. Otherwise, it will consider the array flattened. In this case, if we provide axis=0, it returns an array containing the smallest element for each column. If axis=1, it returns an array containing the smallest element from each row.
  3. out -> This is an optional field. This indicates an alternative output array in which the result is placed. 
  4. keepdims -> This is an optional field. If this is set to True, the reduced axis is left as dimensions with size one. With this option, the result will broadcast correctly against an input array. If a default value is passed, then keepdims will not be passed through to all methods of sub-classes of ndarray; however, any non-default value will be. Any exceptions will be raised if the sub-classes sum method does not implement keepdims.

Return Value

This function returns the minimum of an array of two types.

  1. Scaler -> If the axis is mentioned, None.
  2. Array -> Of dimension arr.ndim-1 if axis is mentioned.

Programming Example

Finding a minimum value of the 1D array

#I mporting numpy
import numpy as np

# We will create an 1D array
arr = np.array([47, 20, 41, 63, 21, 4, 74])
# Printing the array
print("The array is: ", arr)
# Shape of the array
print("Shape of the array is : ", np.shape(arr))

# Now we will print min value of this array
print("Minimum value of the given array is: ", np.amin(arr))

Output

The array is:  [47 20 41 63 21  4 74]
Shape of the array is :  (7,)
Minimum value of the given array is:  4

Explanation

We have first declared an array in this program, with some

of the users giving random numbers. Then we have printed the shape (size) of the array. Then we have called amin() to get the minimum element from the array. We can see that the minimum element of this array is 4, and the output is also 4.

Finding the minimum value of Multidimensional Array.

#Importing numpy
import numpy as np

# We will create a 2D array
# Of shape 4x3
arr = np.array([(14, 2, 34), (41, 5, 46), (71, 38, 29), (50, 57, 52)])
# Printing the array
print("The array is: ")
print(arr)
print("Shape of the array is: ", np.shape(arr))

# Now we will find the minimun value for some cases

# Minimum value of the whole array
print("Minimum value of the whole array is: ", np.amin(arr))

# Minimum value of each row
a = np.amin(arr, axis=1)
print("Minimum value of each row of the array is: ", a)

# Minimum value of each column
b = np.amin(arr, axis=0)
print("Minimum value of each column of the array is: ", b)

Output

The array is: 
[[14  2 34]
 [41  5 46]
 [71 38 29]
 [50 57 52]]
Shape of the array is:  (4, 3)
Minimum value of the whole array is:  2
Minimum value of each row of the array is:  [ 2  5 29 50]
Minimum value of each column of the array is:  [14  2 29]

Explanation

In the above program, we have first declared a matrix of size 4×3; you can see the matrix’s shape, which is (4,3). 

Then we have called amin() to get the output of different cases.

In the first case, we have just passed the arr to amin() function, which returns the minimum value in the whole array because an array is flattened if we do not mention the axis.

In the second case, we have passed arr and axis=1, which returns an array of size 4 containing all the minimum elements from each row.

In the third case, we have passed arr and axis=0, which returns an array of size 3, containing all the minimum elements from each column.

That is it for the np.min() function.

See also

Numpy ones_like()

Numpy full_like()

Numpy diag()

Numpy asmatrix()

Numpy bmat()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.