np.sum和np.add.reduce-在生产中,您使用什么? [英] np.sum and np.add.reduce - in production, what do you use?

查看:141
本文介绍了np.sum和np.add.reduce-在生产中,您使用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为背景,请阅读此快速帖子并明确回答: np.sum和np有什么区别.add.reduce?

As a background, please read this quick post and clear answer: What is the difference between np.sum and np.add.reduce?

因此,对于小型阵列,使用add.reduce更快.让我们以我为学习而尝试的以下代码为例,该代码求和一个2D数组:

So, for a small array, using add.reduce is faster. Let's take the following code which I experimented with for learning, that sums a 2D array:

a = np.array([[1,4,6],[3,1,2]])
print('Sum function result =', np.sum(a))

# faster for small array - 
# print(np.add.reduce(a))

# but the only reduces dimension by 1. So do this repeatedly. I create a copy of x since I keep reducing it:
x = np.copy(a)
while x.size > 1:
    x = np.add.reduce(x)

print('Sum with add.reduce =', x)

因此,以上内容似乎有点过头了-我想最好是在不知道数组大小的情况下,最好使用sum,而且肯定是一个以上的维度.如果您的阵列不明显/很小,是否有人在生产代码中使用add.reduce?如果是这样,为什么?

So, the above seems like overkill - I assume it's better to just use sum when you don't know the size of your array, and definitely if it's more than one dimension. Does anyone use add.reduce in production code if your array isn't obvious/small? If so, why?

欢迎对代码即兴发表任何评论.

Any comments for code improvisation are welcome.

推荐答案

我不认为当np.sumarr.sum会做得同样好时,我没有使用过np.add.reduce.为什么要输入更长的时间以加快速度呢?

I don't think I've used np.add.reduce when np.sum or arr.sum would do just as well. Why type something longer for a trivial speedup.

在适度的大小数组上考虑一个1轴总和:

Consider a 1 axis sum on a modest size array:

In [299]: arr = np.arange(10000).reshape(100,10,5,2)

In [300]: timeit np.sum(arr,axis=0).shape
20.1 µs ± 547 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [301]: timeit arr.sum(axis=0).shape
17.6 µs ± 22.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [302]: timeit np.add.reduce(arr,axis=0).shape
18 µs ± 300 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [303]: 

arr.sum最快.显然,它击败了np.sum,因为它的功能调用少了一层. np.add.reduce并不快.

arr.sum is fastest. Obviously it beats np.sum because there's one less level of function call. np.add.reduce isn't faster.

ufunc.reduce有它的位置,尤其是对于ufunc没有sumprod等效项的地方. (似乎我最近对此发表了评论).

The ufunc.reduce has its place, especially for ufunc that don't have the equivalent of sum or prod. (seems that I commented about this recently).

我怀疑您在SO答案中会发现np.add.atnp.add.reduceat的用途比np.add.reduce更多.这些是ufunc构造,没有等效的方法.

I suspect you'll find more uses of np.add.at or np.add.reduceat than np.add.reduce in SO answers. Those are ufunc constructs that don't have a method equivalent.

或搜索诸如keepdims之类的关键字.这对所有3种构造体都可用,但是几乎所有示例都将它与sum而不是reduce一起使用.

Or search for a keyword like keepdims. That's available with all 3 constructs, but almost all examples will be using it with sum, not reduce.

在设置这些测试时,我偶然发现了一个我没有意识到的差异:

When I was setting up those tests, I stumbled on a difference I wasn't aware of:

In [307]: np.add.reduce(arr).shape    # default axis 0
Out[307]: (10, 5, 2)
In [308]: np.sum(arr)     # default axis None
Out[308]: 49995000
In [309]: arr.sum()
Out[309]: 49995000

这篇关于np.sum和np.add.reduce-在生产中,您使用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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