Python numpy max vs amax vs 最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33569668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
numpy max vs amax vs maximum
提问by DilithiumMatrix
numpy has three different functions which seem like they can be used for the same things --- except that numpy.maximum
can onlybe used element-wise, while numpy.max
and numpy.amax
can be used on particular axes, or all elements. Why is there more than just numpy.max
? Is there some subtlety to this in performance?
numpy的具有看起来他们可被用于同样的东西三个不同的函数---不同之处在于numpy.maximum
可仅被用于逐元素,而numpy.max
且numpy.amax
可以在特定轴,或所有元件一起使用。为什么不止如此numpy.max
?这在性能上有什么微妙之处吗?
(Similarly for min
vs. amin
vs. minimum
)
(类似于min
vs. amin
vs. minimum
)
采纳答案by Alex Riley
np.max
is just an alias for np.amax
. This function only works on a singleinput array and finds the value of maximum element in that entire array (returning a scalar). Alternatively, it takes an axis
argument and will find the maximum value along an axis of the input array (returning a new array).
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])
The default behaviour of np.maximum
is to take twoarrays and compute their element-wise maximum. Here, 'compatible' means that one array can be broadcast to the other. For example:
的默认行为np.maximum
是采用两个数组并计算它们的元素最大值。在这里,“兼容”意味着一个数组可以广播到另一个数组。例如:
>>> b = np.array([3, 6, 1])
>>> c = np.array([4, 2, 9])
>>> np.maximum(b, c)
array([4, 6, 9])
But np.maximum
is also a universal functionwhich means that it has other features and methods which come in useful when working with multidimensional arrays. For example you can compute the cumulative maximum over an array (or a particular axis of the array):
但np.maximum
也是一个通用函数,这意味着它具有在处理多维数组时有用的其他功能和方法。例如,您可以计算数组(或数组的特定轴)上的累积最大值:
>>> d = np.array([2, 0, 3, -4, -2, 7, 9])
>>> np.maximum.accumulate(d)
array([2, 2, 3, 3, 3, 7, 9])
This is not possible with np.max
.
这是不可能的np.max
。
You can make np.maximum
imitate np.max
to a certain extent when using np.maximum.reduce
:
使用时可以在一定程度上进行np.maximum
模仿:np.max
np.maximum.reduce
>>> np.maximum.reduce(d)
9
>>> np.max(d)
9
Basic testing suggests the two approaches are comparable in performance; and they should be, as np.max()
actually calls np.maximum.reduce
to do the computation.
基本测试表明这两种方法的性能相当;他们应该是,因为np.max()
实际上调用np.maximum.reduce
进行计算。
回答by tmdavison
You've already stated why np.maximum
is different - it returns an array that is the element-wise maximum between two arrays.
您已经说明了为什么np.maximum
不同 - 它返回一个数组,该数组是两个数组之间的元素最大值。
As for np.amax
and np.max
: they both call the same function - np.max
is just an alias for np.amax
, and they compute the maximum of all elements in an array, or along an axis of an array.
至于np.amax
and 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>
回答by YaOzI
For completeness, in Numpy there are four maximumrelated functions. They fall into two different categories:
为了完整起见,在 Numpy 中有四个最大的相关函数。它们分为两个不同的类别:
np.amax/np.max
,np.nanmax
: for single arrayorder statistics- and
np.maximum
,np.fmax
: for element-wise comparison of two arrays
np.amax/np.max
,np.nanmax
: 用于单数组顺序统计- and
np.maximum
,np.fmax
: 用于两个数组的逐元素比较
I. For single arrayorder statistics
一、对于单数组顺序统计
NaNs propagator np.amax/np.max
and its NaN ignorant counterpart np.nanmax
.
NaN 传播器np.amax/np.max
及其 NaN 无知对应物np.nanmax
。
np.max
is just an alias ofnp.amax
, so they are considered as one function.>>> np.max.__name__ 'amax' >>> np.max is np.amax True
np.max
propagates NaNs whilenp.nanmax
ignores NaNs.>>> np.max([np.nan, 3.14, -1]) nan >>> np.nanmax([np.nan, 3.14, -1]) 3.14
np.max
只是 的别名np.amax
,因此它们被视为一个函数。>>> np.max.__name__ 'amax' >>> np.max is np.amax True
np.max
传播 NaN 而np.nanmax
忽略 NaN。>>> np.max([np.nan, 3.14, -1]) nan >>> np.nanmax([np.nan, 3.14, -1]) 3.14
II. For element-wise comparison of two arrays
二、用于两个数组的逐元素比较
NaNs propagator np.maximum
and its NaNs ignorant counterpart np.fmax
.
NaNs 传播器np.maximum
和它的 NaNs 无知对应物np.fmax
。
Both functions require two arrays as the first two positional args to compare with.
# x1 and x2 must be the same shape or can be broadcast np.maximum(x1, x2, /, ...); np.fmax(x1, x2, /, ...)
np.maximum
propagates NaNs whilenp.fmax
ignores NaNs.>>> 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])
The element-wise functions are
np.ufunc
(Universal Function), which means they have some special properties that normal Numpy function don't have.>>> type(np.maximum) <class 'numpy.ufunc'> >>> type(np.fmax) <class 'numpy.ufunc'> >>> #---------------# >>> type(np.max) <class 'function'> >>> type(np.nanmax) <class 'function'>
这两个函数都需要两个数组作为要比较的前两个位置参数。
# x1 and x2 must be the same shape or can be broadcast np.maximum(x1, x2, /, ...); np.fmax(x1, x2, /, ...)
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])
逐元素函数是
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'>
And finally, the same rules apply to the four minimumrelated functions:
最后,同样的规则适用于四个最小相关函数:
np.amin/np.min
,np.nanmin
;- and
np.minimum
,np.fmin
.
np.amin/np.min
,np.nanmin
;- 和
np.minimum
,np.fmin
。
回答by shivaraj karki
np.maximum
not only compares elementwise but also compares array elementwise with single value
np.maximum
不仅按元素进行比较,还可以将数组元素与单个值进行比较
>>>np.maximum([23, 14, 16, 20, 25], 18)
array([23, 18, 18, 20, 25])