Trying to vectorize iterative calculation with numpy

前端 未结 3 644
天命终不由人
天命终不由人 2021-01-05 05:15

I am trying to make some piece of code more efficient by using the vectorized form in numpy. Let me show you an example so you know what I mean.

Given the following

3条回答
  •  萌比男神i
    2021-01-05 05:37

    Numpy's vector calculations act on the vector, not as a sequence of steps, so you have to vectorize the entire expression. For example:

    np.multiply(np.arange(1,5), 2**np.arange(0,4)[np.newaxis].T)
    

    To address the "final" question, yes you have to keep the for loop if you want to do a sequential calculation. You might make it more efficient with map or [... for ...] but optimizing that way takes a lot of trial and error. The beauty of thinking in vectorial terms and using Numpy to implement is that you get a result efficiently without all the trial and error.

    The cumsum and cumprod functions can do something similar to what you're asking for. Instead of 2**np.arange(...), you can get the same thing from

    np.multiply(np.arange(1,5), np.cumprod([1,2,2,2,])[np.newaxis].T)
    

提交回复
热议问题