0

I have multiple 5x5 arrays which are contained within one large array - the overarching shape is: 5 x 5 x 29. I want to sum every 5 x 5 array to produce one single array, instead of 29 single arrays.

I know that you can do something along the lines of:

new_data = data1[:,:,0] + data1[:,:,1] + ... + data1[:,:,29]

However, this gets very cumbersome for large arrays. Is there an easier way to do this?

1
  • 1
    This is the very thing loops were designed for. Commented Jul 31, 2019 at 19:17

3 Answers 3

6

Assuming you are using NumPy, you should be able to do this with:

In [13]: data1 = np.arange(100).reshape(5, 5, 4) # For example

In [14]: data1[:,:,0] + data1[:,:,1] + data1[:,:,2] + data1[:,:,3] # Bad way
Out[14]:
array([[  6,  22,  38,  54,  70],
       [ 86, 102, 118, 134, 150],
       [166, 182, 198, 214, 230],
       [246, 262, 278, 294, 310],
       [326, 342, 358, 374, 390]])

In [15]: data1.sum(axis=2) # Good way
Out[15]:
array([[  6,  22,  38,  54,  70],
       [ 86, 102, 118, 134, 150],
       [166, 182, 198, 214, 230],
       [246, 262, 278, 294, 310],
       [326, 342, 358, 374, 390]])
Sign up to request clarification or add additional context in comments.

4 Comments

What about for floating point numbers? This only seems to be adding integers.
@M.Simpson I'm not sure what you mean. This should work regardless of data type. Could you give an example?
a = [[ 1.5 1.5 1.5] [1.5 1.5 1.5] [1.5 1.5 1.5]] b = [[ 1.5 1.5 1.5] [1.5 1.5 1.5] [1.5 1.5 1.5]] both = np.zeros([3,3,2]) both[:,:,0] = a both[:,:,1] = b print both.sum(axis=3) [[ 2 2 2] [2 2 2] [2 2 2]] Which works fine. However, a = [[ 1 1 1] [1 1 1] [1 1 1]] b = [[ 1 1 1] [1 1 1] [1 1 1]] both = np.zeros([3,3,2]) both[:,:,0] = a both[:,:,1] = b print both.sum(axis=3) [[ 2 2 2] [2 2 2] [2 2 2]] When it should be 3's instead of 2's
@M.Simpson Sorry, it is a little hard to read. Could you update your question or use something like Pastebin instead?
1

If you are saying you have a list of arrays, then use a for loop.

for i in range(29):
    new_data+= data1[:,:,i]

If you are saying you have a tensor or some ND array you should review and research numpy's ND array docs.

1 Comment

What is the definition of new_data?
1

You can use a for loop. Like this:

import numpy as np

new_data = np.zeros((5, 5))
for i in range(29):
    new_data += data1[:,:,i] 

8 Comments

This likely won't work because new_data is a Python list, and += will just append.
No, you are still appending to the list. In fact, now you are making unnecessary copies of the list. Anyway, OP is using arrays. Lists and arrays don't mix well.
What is the definition of new_data?
They likely already have a definition for the array of new_data.
I am not so sure that is true. It looks like a new variable to me. Anyway, you could make it a 5x5 array of zeroes with np.zeros :)
|

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.