1

For the sake of speeding up my algorithm that has numpy arrays with tens of thousands of elements, I'm wondering if I can reduce the time used by numpy.delete().

In fact, if I can just eliminate it? I have an algorithm where I've got my array alpha. And this is what I'm currently doing:

alpha = np.delete(alpha, 0)
beta = sum(alpha)

But why do I need to delete the first element? Is it possible to simply sum up the entire array using all elements except the first one? Will that reduce the time used in the deletion operation?

user961627
  • 12,379
  • 42
  • 136
  • 210

1 Answers1

3

Avoid np.delete whenever possible. It returns a a new array, which means that new memory has to be allocated, and (almost) all the original data has to be copied into the new array. That's slow, so avoid it if possible.

beta = alpha[1:].sum()

should be much faster.

Note also that sum(alpha) is calling the Python builtin function sum. That's not the fastest way to sum items in a NumPy array.

alpha[1:].sum() calls the NumPy array method sum which is much faster.


Note that if you were calling alpha.delete in a loop, then the code may be deleting more than just the first element from the original alpha. In that case, as Sven Marnach points out, it would be more efficient to compute all the partial sums like this:

np.cumsum(alpha[:0:-1])[::-1]
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Thanks. I wasn't actually using sum though, I just put it up to demonstrate any array calculation. In my code I'm actually using logsumexp() (which is from the scipy library). Do you happen to know this? http://stackoverflow.com/questions/25651480/does-scipy-logsumexp-deal-with-the-underflow-challenge – user961627 Sep 03 '14 at 19:15
  • Summing over `alpha[idx:]` for each iteration is rather inefficient, since you are summing the whole tail of the array over and again. You could avoid that by using the (linear-time) `numpy.cumsum(alpha[::-1])[::-1]` instead, which computes the cumulative sums starting from the end in a single step. – Sven Marnach Sep 03 '14 at 19:24