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.
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.
numpy.amin(arr, axis=None, out=None, keepdims=<no value>, initial=<no value>)
The Numpy amin() function takes up to 4 arguments: the following.
- arr -> This is the array from which we can find the min value
- 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.
- out -> This is an optional field. This indicates an alternative output array in which the result is placed.
- 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.
This function returns the minimum of an array of two types.
- Scaler -> If the axis is mentioned, None.
- Array -> Of dimension arr.ndim-1 if axis is mentioned.
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))
The array is: [47 20 41 63 21 4 74] Shape of the array is : (7,) Minimum value of the given array is: 4
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)
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]
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.