0

I have the following 3D numpy.array of shape (2,3,3):

import numpy as np

arr = np.array(((((1,2,3),(9,8,7),(6,5,4))),
                (((10,20,30),(90,80,70),(60,50,40)))))

I want to sum the first dimension and get one 2D array of shape (3,3).

Expected output:

array([[11, 22, 33],
       [99, 88, 77],
       [66, 55, 44]])

I know I can iterate over the elements in the first dimension and sum, with something like this:

for el in range(len(arr)):
    if el == 0:
        arr_sum = arr[el]
    else:
        arr_sum += arr[el]

But there is another option to do this?

Thanks in advance.

1
  • 3
    Just use arr.sum(0) Commented Oct 27, 2021 at 22:44

1 Answer 1

3

You can use, NumPy.sum method.

import numpy as np
arr = np.array(((((1,2,3),(9,8,7),(6,5,4))),(((10,20,30),(90,80,70),(60,50,40)))))

print(np.sum(arr, axis=0))
Sign up to request clarification or add additional context in comments.

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.