Solution 1 np.add.reduce()
You can use the attribute reduce np.add:
a = np.array([100, 100])
b = np.array([200, 200])
c = np.array([1000, 2000])
L = [a, b, c]
np.add.reduce(L)
leads to:
array([1300, 2300])
An entire universal function that takes two arguments has an attribute reducethat applies this function as reduce, ie:
np.add.reduce(L)
becomes:
np.add(np.add(L[0], L[1]), L[2])
np.add, L .
:
:
reduce(a, axis=0, dtype=None, out=None, keepdims=False)
a , ufunc .
2 np.sum()
np.sum :
>>> np.sum(L, axis=0)
array([1300, 2300
, , .
:
a = np.array([100, 100])
b = np.array([200, 200])
c = np.array([1000, 2000])
L = [a, b, c, a, b, c, a, b, c]
reduce :
%timeit np.sum(L, axis=0)
10000 loops, best of 3: 20.7 µs per loop
%timeit np.add.reduce(L)
100000 loops, best of 3: 15.7 µs per loop
:
size = int(1e6)
a = np.random.random(size)
b = np.random.random(size)
c = np.random.random(size)
L = [a, b, c, a, b, c, a, b, c]
:
%timeit np.sum(L, axis=0)
10 loops, best of 3: 41.5 ms per loop
%timeit np.add.reduce(L)
10 loops, best of 3: 41.9 ms per loop