Ale*_*ley 124
np.max
只是一个别名np.amax
.此函数仅适用于单个输入数组,并在整个数组中查找最大元素的值(返回标量).或者,它接受一个axis
参数,并沿输入数组的轴找到最大值(返回一个新数组).
>>> a = np.array([[0, 1, 6],
[2, 4, 1]])
>>> np.max(a)
6
>>> np.max(a, axis=0) # max of each column
array([2, 4, 6])
Run Code Online (Sandbox Code Playgroud)
默认行为np.maximum
是采用两个数组并计算其元素最大值.这里,'兼容'意味着一个阵列可以广播到另一个阵列.例如:
>>> b = np.array([3, 6, 1])
>>> c = np.array([4, 2, 9])
>>> np.maximum(b, c)
array([4, 6, 9])
Run Code Online (Sandbox Code Playgroud)
但np.maximum
它也是一个通用函数,这意味着它具有在处理多维数组时有用的其他特性和方法.例如,您可以计算数组(或数组的特定轴)的累积最大值:
>>> d = np.array([2, 0, 3, -4, -2, 7, 9])
>>> np.maximum.accumulate(d)
array([2, 2, 3, 3, 3, 7, 9])
Run Code Online (Sandbox Code Playgroud)
这是不可能的np.max
.
使用时,您可以在一定程度上np.maximum
模仿:np.max
np.maximum.reduce
>>> np.maximum.reduce(d)
9
>>> np.max(d)
9
Run Code Online (Sandbox Code Playgroud)
基本测试表明这两种方法在性能上具有可比性; 它们应该像np.max()
实际上要求np.maximum.reduce
进行计算一样.
tmd*_*son 16
您已经说明了为什么np.maximum
不同 - 它返回一个数组,它是两个数组之间的元素最大值.
至于np.amax
和np.max
:它们都调用相同的函数 - np.max
只是一个别名np.amax
,它们计算数组中所有元素的最大值,或者沿着数组的轴.
In [1]: import numpy as np
In [2]: np.amax
Out[2]: <function numpy.core.fromnumeric.amax>
In [3]: np.max
Out[3]: <function numpy.core.fromnumeric.amax>
Run Code Online (Sandbox Code Playgroud)
YaO*_*OzI 11
为了完整起见,在 Numpy 中有四个最大的相关函数。它们分为两个不同的类别:
np.amax/np.max
, np.nanmax
: 用于单数组顺序统计np.maximum
, np.fmax
: 用于两个数组的逐元素比较NaN 传播器np.amax/np.max
及其 NaN 无知对应物np.nanmax
。
np.max
只是 的别名np.amax
,因此它们被视为一个函数。
>>> np.max.__name__
'amax'
>>> np.max is np.amax
True
Run Code Online (Sandbox Code Playgroud)np.max
传播 NaN 而np.nanmax
忽略 NaN。
>>> np.max([np.nan, 3.14, -1])
nan
>>> np.nanmax([np.nan, 3.14, -1])
3.14
Run Code Online (Sandbox Code Playgroud)NaNs 传播器np.maximum
和它的 NaNs 无知对应物np.fmax
。
这两个函数都需要两个数组作为要比较的前两个位置参数。
# x1 and x2 must be the same shape or can be broadcast
np.maximum(x1, x2, /, ...);
np.fmax(x1, x2, /, ...)
Run Code Online (Sandbox Code Playgroud)np.maximum
传播 NaN 而np.fmax
忽略 NaN。
>>> np.maximum([np.nan, 3.14, 0], [np.NINF, np.nan, 2.72])
array([ nan, nan, 2.72])
>>> np.fmax([np.nan, 3.14, 0], [np.NINF, np.nan, 2.72])
array([-inf, 3.14, 2.72])
Run Code Online (Sandbox Code Playgroud)逐元素函数是np.ufunc
( Universal Function ),这意味着它们具有一些普通 Numpy 函数没有的特殊属性。
>>> type(np.maximum)
<class 'numpy.ufunc'>
>>> type(np.fmax)
<class 'numpy.ufunc'>
>>> #---------------#
>>> type(np.max)
<class 'function'>
>>> type(np.nanmax)
<class 'function'>
Run Code Online (Sandbox Code Playgroud)最后,同样的规则适用于四个最小相关函数:
np.amin/np.min
, np.nanmin
;np.minimum
,np.fmin
。 归档时间: |
|
查看次数: |
122384 次 |
最近记录: |