In case you wish to find the minimum value from a set of values in an array, you are at the right place. Undoubtedly, numpy.amin is the best numpy function to achieve the required task. Read this article to learn more about this function!
About numpy.amin
The amin() function of numpy provides the minimum value out of a range of numbers in an array, usually along an axis. It has a couple of uses, like finding the minimum value in :
- Signal
- Loss functions
- Temperature map
Syntax of numpy.amin()
The amin function is accepted in the given manner:
numpy.amin(arr, axis=None, out=None, keepdims=False)
It has the following parameters:
- arr
- axis
- out
- keepdims
Here, arr refers to the array that needs to be traversed. The axis attribute specifies the axis, i.e., horizontal or vertical. It can be none in the case of a one-dimensional array. The out parameter is used for the output, while keepdims lets us know if a user wants the same dimensions for the output and input array or not.
Working
Let’s have a look at two codes that elaborate on the working of the amin() function. The first example is for a flattened array, while 2nd one works along the two axes.
import numpy as np
# Create a NumPy array.
array = np.array([1, 2, 3, 4, 5])
# Get the minimum value of the array.
minimum_value = np.amin(array)
# Print the minimum value.
print(minimum_value)
Henceforth, the output will be 1 for this 1-dimensional array. Let’s consider another example to observe the working of amin() along the two axes.
import numpy as np
# Create a NumPy array.
array = np.array([[1, 2], [3, 4]])
# Get the minimum value of the array along the first axis.
minfirst_axis = np.amin(array, axis=0)
# Get the minimum value of the array along the second axis.
minsecond_axis = np.amin(array, axis=1)
# Print the minimum values.
print(minfirst_axis)
print(minsecond_axis)
Here, the preferred output will be:
[1 3]
[1 2]
Illustration of keepdims parameter
As suggested earlier, keepdims can have two boolean values- True or False. In case it has a True value, the dimensions of the input and output array will be the same. In contrast, in the case of False, we will observe reduced dimensions. It is a reduction operation precisely. It is extremely useful in cases where the user wants to broadcast the answer in the same dimensions as the original array after doing a reduction.
import numpy as np
# Create a NumPy array.
arr = np.array([[1, 2], [3, 4]])
# along the 1st axis, with `keepdims` set to `True`.- vertical
with_keepdims = np.amin(arr, axis=0, keepdims=True)
#along the 2nd axis, with `keepdims` set to `False`.- horizontal
without_keepdims = np.amin(arr, axis=1, keepdims=False)
# see the minimum values in both cases
print(with_keepdims)
print(without_keepdims)
Here, the suggested output will be:
[[1 3]]
[1 2]
Return type
It depends on two things:
- input array
- keepdims attribute (True or False)
To check the data type of the array, we use dtype as portrayed in the example:
import numpy as np
# NumPy array of scalar values.
array = np.array([1, 2, 3, 4, 5])
# minimum value of the array.
minimum_value = np.amin(array)
# check the minimum value and its data type.
print(minimum_value, minimum_value.dtype)
Thus, the output is:
1 int32
The preferred dtypes
There are two dtypes that are commonly used with numpy.amin:
np.float64
np.int64
They work accurately for the amin function. However, it works with np.float32
, np.int32
, and np.complex128
also. For large float arrays, np.float64
dtype is used, whereas for small integer arrays, np.int32
dtype is preferred. The given code shows how it can be used with the float64 dtype.
import numpy as np
# Create a NumPy array of floating-point numbers.
array = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=np.float64)
# Get the minimum value of the array.
min = np.amin(array)
# Print the minimum value and its data type.
print(min, min.dtype)
#output:
#1.0 float64
It works similarly for complex numbers. Eg:
import numpy as np
# Create a NumPy array of complex numbers.
array = np.array([1 + 2j, 3 - 4j, 5 + 6j, 7 - 8j], dtype=np.complex128)
# Get the minimum value of the array.
min = np.amin(array)
# Print the minimum value and its data type.
print(min, min.dtype)
#output:
#(1+2j) complex128
The Performance Boost
To get efficient output, the following tips are suggested:
- Use
np.float64
for floating-point arrays,np.int64
for integer arrays - Use it for small arrays.
- Assign a value to
keepdims
an attribute to have just one output value. - np.nanmin() can avoid NaN values if they exist.
These will definitely aid the user in traversing arrays in a better format.
amin for minimum signal
The amin function can be utilized to gauge the minimum signal with both time constraints and space constraints. For example, a user can find the minimum signal over a period of time. Consider the given example for better analysis.
import numpy as np
# Create a signal array.
signal = np.array([[1, 2, 3], [4, 5, 6]])
# Find the minimum value of the signal along the time axis.
min = np.amin(signal, axis=0)
# Print the minimum value.
print(min)
In this example, the minimum signal along the horizontal axis i.e., rows, has been taken. Thus, the output is:
[1 4]
amin for signals with noise
Some signals may not be entirely pure. They might have nan values too. As a result, an extension of np.amin() i.e., np.nanmin() is used. This function aims to have a noise-free processing of signal. The following example explains this well.
import numpy as np
# Create a signal array with noise.
signal = np.array([1, 2, 3, 4, 5, np.nan, 7, 8, 9, 10])
# Find the minimum value of the signal with noise, ignoring the NaN values.
min = np.nanmin(signal)
# Print the minimum value.
print(min)
Thus, Output will be displayed in the given manner as null values have been ignored.
1.0
numpy amin for multidimensional array
The amin function of numpy can be used with multidimensional arrays also. Thus, whether you wish to find a minimum of a group of numbers in a flattened array or an array with greater than one dimension, you can go for the amin function present in the numpy library.
For arrays with more than one dimension, the user is supposed to mention the axis along which he wants to calculate the mean. The numpy library considers the axis as a necessary parameter here. You can find the mean either along the rows or along the columns.
Have a look at the given example to understand how you can work with a multidimensional array in order to find the mean. This particular example considers row-wise traversal of the array.
import numpy as np
# Create a 2D NumPy array.
array = np.array([[1, 2], [3, 4]])
# Find the minimum value of the array along the first axis.
minimum_value = np.amin(array, axis=0)
# Print the minimum value.
print(minimum_value)
Therefore, you will get the output as: [1 3]. Similarly, you can work on 3-dimensional arrays to find the minimum element. In the case of a three-dimensional array, you can specify two axes at the same time, like axis=0,1.
The given example works for a 3 dimensional array. Here, two axes – both horizontal and vertical are considered.
import numpy as np
# Create a 3D NumPy array.
array = np.array([[1, 2, 3], [4, 5, 6]])
# Find the minimum value of the array along the first and second axes.
minimum_value = np.amin(array, axis=(0, 1))
# Print the minimum value.
print(minimum_value)
On final execution of the code, you will get the output as: [1 4]
numpy amin vs. min
A clear difference exists between min and amin functions of numpy. Consider the tabulated form of these differences.
min() | amin() |
---|---|
It is independent of any axis | Provides a scalar value for a 1D array or keepdims is False |
Returns the array only for a multidimensional array or keepdims is True | One can obtain minimum value along a specific axis |
Provides a scalar value | Provides a scalar value for 1D array or keepdims is False |
numpy amin alternatives
There are quite a few alternatives to the amin function of numpy. These are:
- min()
- np.argmin()
If you want the index of the minimum element, go for the argmin() function of numpy. The argmin function accepts the same type of parameter array of numbers. The only difference is that it provides the index of the minimum element apart from the minimum element itself. The example below explains how one can use the argmin() function just like the amin() function of numpy.
import numpy as np
# Create a NumPy array.
array = np.arr([1, 2, 3, 4, 5])
# Find the index of the first minimum element of the array.
min= np.argmin(array)
# Print the minimum value and its index.
print(array[min], min)
FAQs on numpy.amin
In the case of NaN values, use np.namin() function.
Conclusion
This article describes how we can make use of numpy.amin() function. It suggests the related syntax, use cases, and much more.