np.max, np.amax, np.maximum 区 别

2019-07-18  本文已影响0人  默写年华Antifragile

1. np.max, np.amax

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).
-------------------------------------------------------------------------
np.max

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

-----------------------------------------------------------------------------------------
np.amax

>>> a = np.array([[0, 1, 6],
                  [2, 4, 1]])
>>> np.amax(a)
6
>>> np.amax(a, axis=0) # max of each column
array([2, 4, 6])

2. 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 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
上一篇下一篇

猜你喜欢

热点阅读