简体   繁体   English

Python中累加和的矢量化

[英]Vectorization of cumulative sum in python

I'm trying to vectorize/broadcast (Not sure what it is called formally) my code in order to make it faster, but I can't quite get it. 我试图对我的代码进行矢量化/广播(不确定它的正式名称),以便使其更快,但是我不太明白。 What I think I should be using is numpy.cumsum (with axis=0) but I don't know how to get it (fast) in the correct array to use it. 我想我应该使用的是numpy.cumsum(其中axis = 0),但我不知道如何在正确的数组中(快速)使用它。

What I want with this code is basically the absolute sum of l1 for adding each element from l2 to all numbers in l1. 我想要的这段代码基本上是l1的绝对和,用于将l2中的每个元素加到l1中的所有数字上。 So this gives not one answer, but len(l2) amount of answers. 因此,这给出的答案不是一个,而是len(l2)个。 The (non-vectorized) code below give the correct output. 下面的(非矢量化)代码给出正确的输出。

    # l1 and l2 are numpy arrays
    for i in l2:
        l1 += i
        answer = numpy.sum(numpy.absolute(l1))
        print answer

Can anyone provide an answer or hint? 谁能提供答案或提示?

The trick is to first combine the two one-dimensional arrays into a single two-dimensional array, and then sum over that. 诀窍是首先将两个一维数组合并为一个二维数组,然后对其求和。 If you have a vector of shape (a,1) and you broadcast it with an array of shape (b,) , the resulting array will be shape (a,b) . 如果您有一个形状为(a,1)的向量,并使用形状(b,)的数组对其进行广播,则所得数组将为形状(a,b) It's handy to add extra axes with length one into arrays to get this sort of behavior. 将具有长度为1的额外轴添加到数组中以获得这种行为很方便。

Here's a way to get the same answer without loops 这是一种无需循环即可获得相同答案的方法

# Assume l1 has length n1, l2 has length n2
suml2 = np.cumsum(l2)  # length n2
y = l1 + suml2[:,np.newaxis]  # shape (n2, n1)
answer = np.sum(np.abs(y), axis=1)  # shape (n2,)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM