max()
方法返回数组沿轴的最大元素。
示例
import numpy as np
array1 = np.array([10, 12, 14, 11, 5])
# return the largest element
maxValue= np.max(array1)
print(maxValue)
# Output: 14
max() 语法
max()
的语法是
numpy.max(array, axis = None, out = None, keepdims = <no value>, initial=<no value>, where=<no value>)
max() 参数
max()
方法接受六个参数
array
- 输入数组axis
(可选) - 返回最大值的轴 (int
)out
(可选) - 用于存储输出的数组keepdims
(可选) - 是否保留输入数组的维度 (bool
)initial
(可选) - 输出元素的最大值 (标量)where
(可选) - 包含在最大值计算中的元素 (bool
的array
)
max() 返回值
max()
方法返回最大的元素。
注意:如果输入数组中至少有一个元素是 NaN
,则 max()
返回 NaN
。
示例 1:带 2D 数组的 max()
axis
参数定义了我们如何处理 2D 数组中的最大元素。
- 如果
axis
=None
,数组将被展平,并返回展平数组的最大值。 - 如果
axis
= 0,则返回每列中最大元素的最大值。 - 如果
axis
= 1,则返回每行中最大元素的最大值。
import numpy as np
array = np.array([[10, 17, 25],
[15, 11, 22]])
# return the largest element of the flattened array
maxValue = np.max(array)
print('The largest element in the flattened array: ', maxValue)
# return the largest element in each column
maxValue = np.max(array, axis = 0)
print('The largest element in each column (axis 0): ', maxValue)
# return the largest element in each row
maxValue = np.max(array, axis = 1)
print('The largest element in each row (axis 1): ', maxValue)
输出
The largest element in the flattened array: 25 The largest element in each column (axis 0): [15 17 25] The largest element in each row (axis 1): [25 22]
示例 2:使用 out 将结果存储在所需位置
在我们之前的示例中,max()
函数生成了一个新的输出数组。
但是,我们可以使用 out
参数使用现有数组来存储输出。
import numpy as np
array1 = np.array([[10, 17, 25],
[15, 11, 22],
[11, 19, 20]])
# create an empty array
array2= np.array([0, 0, 0])
# pass the 'out' argument to store the result in array2
np.max(array1, axis = 0, out = array2)
print(array2)
输出
[15 19 25]
示例 3:带 keepdims 的 max()
当 keepdims = True
时,结果数组的维度与输入数组的维度匹配。
import numpy as np
array1 = np.array([[10, 17, 25],
[15, 11, 22]])
print('Dimensions of original array: ', array1.ndim)
maxValue = np.max(array1, axis = 1)
print('\n Without keepdims: \n', maxValue)
print('Dimensions of array: ', maxValue.ndim)
# set keepdims to True to retain the dimension of the input array
maxValue = np.max(array1, axis = 1, keepdims = True)
print('\n With keepdims: \n', maxValue)
print('Dimensions of array: ', maxValue.ndim)
输出
Dimensions of original array: 2 Without keepdims: [25 22] Dimensions of array: 1 With keepdims: [[25] [22]] Dimensions of array: 2
如果不使用 keepdims
,结果只是一个包含索引的一维数组。
使用 keepdims
时,结果数组具有与输入数组相同的维度数。
示例 4:带 initial 的 max()
我们使用 initial
来定义 max()
可以返回的最大值。如果数组的最大值小于初始值,则返回 initial
。
import numpy as np
# max value > initial, returns max value
array1 = np.array([[10, 25, 17, 16, 14]])
maxValue = np.max(array1, initial = 6)
print(maxValue)
# max value < initial, returns initial
array2 = np.array([[10, 25, 17, 16, 14]])
maxValue = np.max(array2, initial = 26)
print(maxValue)
# in case of an empty array, initial value is returned
array3 = np.array([])
maxValue = np.max(array3, initial = 5)
print(maxValue)
输出
25 26 5.0
示例 5:在 max() 中使用 where 参数
可选参数 where
指定要包含在最大值计算中的元素。
import numpy as np
arr = np.array([[12, 25, 32],
[47, 50, 36]])
# take max of entire array
result1 = np.max(arr)
# max of only odd elements
result2 = np.max(arr, initial = 0, where = (arr%2==1))
# max of numbers less than 30
result3 = np.max(arr, initial = 0,where = (arr < 30))
print('Max of entire array:', result1)
print('Max of only odd elements:', result2)
print('Max of numbers less than 30:', result3)
输出
Max of entire array: 50 Max of only odd elements: 47 Max of numbers less than 30: 25