minimum()
函数用于查找两个数组对应元素之间的最小值。
import numpy as np
array1 = np.array([1, 2, 3, 4, 5])
array2 = np.array([2, 4, 1, 5, 3])
# find the element-wise minimum of array1 and array2
result = np.minimum(array1, array2)
print(result)
# Output: [1 2 1 4 3]
minimum() 语法
minimum()
的语法是:
numpy.minimum(array1, array2, out = None)
minimum() 参数
minimum()
函数接受以下参数:
array1
和array2
- 要比较的两个输入数组out
(可选) - 用于存储结果的输出数组
minimum() 返回值
minimum()
函数返回一个新数组,其中包含两个数组对应元素之间的最小值。
示例 1:minimum() 与 2-D 数组
import numpy as np
# create two 2-D arrays
array1 = np.array([[1, 2, 3], [4, 5, 6]])
array2 = np.array([[2, 4, 1], [5, 3, 2]])
# find the element-wise minimum of array1 and array2
result = np.minimum(array1, array2)
print(result)
输出
[[1 2 1] [4 3 2]]
在此,minimum()
比较 array1
和 array2
的对应元素,并返回一个新数组 result,其中包含每个位置的最小值。
示例 2:minimum() 中 out 参数的使用
import numpy as np
array1 = np.array([1, 3, 5, 7, 9])
array2 = np.array([2, 4, 6, 8, 10])
# create an empty array with the same shape as array1
output = np.empty_like(array1)
# compute the element-wise minimum and store the result in output
np.minimum(array1, array2, out=output)
print(output)
输出
[1 3 5 7 9]
在此,在指定 out=output
后,逐元素最小化操作的结果存储在 output
数组中。