numpy max vs amax vs maximum [英] numpy max vs amax vs maximum

查看:158
本文介绍了numpy max vs amax vs maximum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy具有三种不同的功能,它们似乎可以用于相同的目的---除了numpy.maximum仅可以 用于元素方式,而numpy.maxnumpy.amax可以在特定轴或所有元素上使用.为什么不只是numpy.max?在性能上有一些微妙之处吗?

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?

(类似于min vs. amin vs. minimum)

(Similarly for min vs. amin vs. minimum)

推荐答案

np.max只是np.amax的别名.此函数仅在单个输入数组上起作用,并在整个数组中找到最大元素的值(返回标量).或者,它使用一个axis参数,并将沿着输入数组的轴找到最大值(返回一个新数组).

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

np.maximum的默认行为是采用两个数组并计算其按元素的最大值.在这里,兼容"意味着可以将一个阵列广播到另一个阵列.例如:

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

但是np.maximum也是通用函数,这意味着它具有其他功能和方法,在处理多维数组时很有用.例如,您可以计算数组(或数组的特定轴)上的累积最大值:

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

这对于np.max是不可能的.

使用np.maximum.reduce时,可以使np.maximum在一定程度上模仿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

基本测试表明这两种方法在性能上可比;它们应该是 np.max()实际上调用np.maximum.reduce 进行计算.

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.

这篇关于numpy max vs amax vs maximum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆