您好,欢迎来到Numpy amin教程。在本教程中,我们将学习NumPy amin()方法,并看到许多相关示例。那么让我们开始吧!
另请阅读:Numpy.subtract():如何在 Python 中使用 NumPy 进行减法?
什么是 NumPy amin?
NumPy 中的 amin 方法是一个返回数组元素最小值的函数。它可以是所有数组元素的最小值、沿行的数组元素的最小值或沿列的数组元素的最小值。
我们将在本教程的后续部分中看到每个示例的示例。
NumPy amin 的语法
numpy.amin(a, axis = None , out = None ) |
范围 | 描述 | 必需/可选 |
一个(类似数组) | 输入数据。 | 必需的 |
轴 | 计算数组最小值的轴。它可以是 axis=0 或 axis=1 或 axis=None ,这意味着要返回展平数组的最小值。 | 选修的 |
出去 | 用于放置结果的替代输出数组。它必须具有与预期输出相同的形状。 | 选修的 |
返回:a中的最小元素。如果axis=None,则输出是标量,否则输出是数组。
numpy.amin() 的示例
让我们直接进入使用 numpy.amin() 函数的不同示例。
当数组是一维时使用 numpy.amin()
import numpy as np a = [ 10 , 3 , 25 ] ans = np.amin(a) print ( "a =" , a) print ( "Minimum of a =" , ans) |
输出:
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.