Anake
Anake

Reputation: 7659

Collapse nD numpy array into a 1D array

I am trying to sum the values of a nD array along a particular axis to effectively collapse it into a 1D array. I have been looking through the docs but haven't been able to find the right function. I will try to explain my question better with some code:

In [46]: g
Out[46]: 
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

The output I need is:

array([5,10,15])

My actual data is a 7 MB file so I don't really want to use a for loop.

Thank you for your help

Upvotes: 2

Views: 468

Answers (1)

tom10
tom10

Reputation: 69242

Just doing

numpy.sum(g, axis=0)

should work.

Upvotes: 6

Related Questions