Reputation: 1681
Let's say I got this
a = np.arange(9).reshape((3,3))
I want get a numpy array of [9,12,15]
which is a result of
[0+3+6, 1+4+7, 2+5+8]
Upvotes: 4
Views: 236
Reputation: 6194
Use the numpy.sum
function and specify the axis over which you want to sum, which is 0
in your case:
import numpy as np
a = np.arange(9).reshape((3,3))
a_sum = np.sum(a, axis=0)
print a_sum
This gives you:
[ 9 12 15]
The answer of Kasramvd uses the object-oriented approach, which some people prefer:
a_sum = a.sum(axis=0)
Upvotes: 4
Reputation: 107337
You can usenumpy.array.sum()
function by passing the axis=0
:
>>> a.sum(axis=0)
array([ 9, 12, 15])
Upvotes: 5