Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

If I have a numpy array like this (8 two dimensional sub arrays):

 array([[[1, 1, 1], 

         [1, 1, 1]],

        [[2, 2, 2], 

         [2, 2, 2]], 

        [[1, 1, 1], 

         [1, 1, 1]], 

        [[2, 2, 2], 

         [2, 2, 2]], 

        [[3, 3, 3], 

         [3, 3, 3]], 

        [[4, 4, 4], 

         [4, 4, 4]], 

        [[3, 3, 3], 

         [3, 3, 3]], 

        [[4, 4, 4], 

         [4, 4, 4]]])

How do I average every four two dimensional arrays such that I get a new array that looks like this:

 array([[[1.5, 1.5, 1.5],

         [1.5, 1.5, 1.5]],

        [[3.5, 3.5, 3.5], 

         [3.5, 3.5, 3.5]]])

1 Answer

0 votes
by (25.1k points)

Assuming a to be the input array.you can split the first axis into two with the length of the second one being 4 resulting in n+1-dim array and then get average along that one. Like this:

a.reshape((-1,4)+a.shape[1:]).mean(1)

Browse Categories

...