## 问题：numpy max vs amax vs maximum

numpy的具有看起来他们可被用于同样的东西三个不同的函数—不同之处在于`numpy.maximum`被用于逐元素，而`numpy.max``numpy.amax`可以在特定轴，或所有元件一起使用。为什么不仅仅存在`numpy.max`？在性能上有一些微妙之处吗？

（类似`min`vs. `amin`vs. `minimum`

numpy has three different functions which seem like they can be used for the same things — except that `numpy.maximum` can only be 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?

(Similarly for `min` vs. `amin` vs. `minimum`)

## 回答 0

`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])``````

``````>>> b = np.array([3, 6, 1])
>>> c = np.array([4, 2, 9])
>>> np.maximum(b, c)
array([4, 6, 9])``````

``````>>> d = np.array([2, 0, 3, -4, -2, 7, 9])
>>> np.maximum.accumulate(d)
array([2, 2, 3, 3, 3, 7, 9])``````

``````>>> np.maximum.reduce(d)
9
>>> np.max(d)
9``````

`np.max` is just an alias for `np.amax`. This function only works on a single input 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).

``````>>> 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 two arrays and compute their element-wise maximum. Here, ‘compatible’ means that one array can be broadcast to the other. For example:

``````>>> 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 function which 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):

``````>>> 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`.

You can make `np.maximum` imitate `np.max` to a certain extent when using `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.

## 回答 1

``````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>``````

You’ve already stated why `np.maximum` is different – it returns an array that is the element-wise maximum between two arrays.

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.

``````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>
``````

## 回答 2

• `np.amax/np.max``np.nanmax`：：用于单阵列订单统计
• `np.maximum``np.fmax`：用于两个数组的元素比较

### 一，单阵列订单统计

NaNs传播者`np.amax/np.max`及其NaN无知对应物`np.nanmax`

• `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``````

### 二。用于两个数组的元素比较

NaNs传播者`np.maximum`及其NaNs无知对应物`np.fmax`

• 这两个函数都需要两个数组作为要比较的前两个位置args。

``````# 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'>``````

• `np.amin/np.min``np.nanmin`;
• 并且`np.minimum``np.fmin`

For completeness, in Numpy there are four maximum related functions. They fall into two different categories:

• `np.amax/np.max`, `np.nanmax`: for single array order statistics
• and `np.maximum`, `np.fmax`: for element-wise comparison of two arrays

### I. For single array order statistics

NaNs propagator `np.amax/np.max` and its NaN ignorant counterpart `np.nanmax`.

• `np.max` is just an alias of `np.amax`, so they are considered as one function.

``````>>> np.max.__name__
'amax'
>>> np.max is np.amax
True
``````
• `np.max` propagates NaNs while `np.nanmax` ignores NaNs.

``````>>> 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`.

• 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 while `np.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'>
``````

And finally, the same rules apply to the four minimum related functions:

• `np.amin/np.min`, `np.nanmin`;
• and `np.minimum`, `np.fmin`.

## 回答 3

`np.maximum` 不仅按元素进行比较，而且将数组与单个值进行比较

``````>>>np.maximum([23, 14, 16, 20, 25], 18)
array([23, 18, 18, 20, 25])``````

`np.maximum` not only compares elementwise but also compares array elementwise with single value

``````>>>np.maximum([23, 14, 16, 20, 25], 18)
array([23, 18, 18, 20, 25])
``````