9

I'd like to know if there is a more efficient/pythonic way to add multiple numpy arrays (2D) rather than:

def sum_multiple_arrays(list_of_arrays):
   a = np.zeros(shape=list_of_arrays[0].shape) #initialize array of 0s
   for array in list_of_arrays:
      a += array
   return a 

Ps: I am aware of np.add() but it works only with 2 arrays.

2
  • np.sum(list_of_arrays, axis=0) should work. Or np.add.reduce(list_of_arrays). Commented Feb 9, 2021 at 1:23
  • Thank you very much. Please let it be an answer so i can accept Commented Feb 9, 2021 at 1:26

3 Answers 3

14
np.sum(list_of_arrays, axis=0) 

should work. Or

np.add.reduce(list_of_arrays)
Sign up to request clarification or add additional context in comments.

Comments

5

The simplest and most Pythonic solution is simply to use sum(), like so:

sum(list_of_arrays)

This may even be faster than other options, at least under some conditions (thanks to Loc Quan for pointing this out!). For example, on my laptop, with Python 3.10.2 and NumPy 2.1.3, I ran the following code:

n = 100000; m = 5; k = 5;
list_of_arrays = [np.random.random_sample((n,m)) for _ in range(k)]

%timeit np.sum(list_of_arrays, axis=0)
7.29 ms ± 459 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit np.add.reduce(list_of_arrays)
7.04 ms ± 134 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit reduce(np.add, list_of_arrays)
2.04 ms ± 86.2 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit sum(list_of_arrays)
1.59 ms ± 44.9 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)

Showing a huge advantage to sum() over np.sum() and np.add.reduce(), and a more modest but still significant speedup vs. reduce(np.add, ...).

3 Comments

doesnt work for more than 2 arrays.
@DaddyKropotkin Seems to work for me, for example: sum([np.array([0,1]), np.array([1,0]), np.array([1.1, 1.1])])
Surprisingly on NumPy 1.26.4 sum() runs faster than np.sum(..., axis=0) and np.add.reduce() by ~25% (1.4ms compared to 1.8ms) for my list of 5 arrays of ~100000 rows and 5 columns. I have checked again with a list of np.random.sample((100_000, 5)) arrays and same results.
0

i know the question says numpy, but here's a pure python answer in case someone needs it.

map(sum, zip(a,b)) which returns a generator. you can also do list(map(sum, zip(a,b))) to get a list

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.