Skip to content Skip to sidebar Skip to footer

Block Mean Of 2d Numpy Array (in Both Dimensions)

This question is related to Block mean of numpy 2D array (in fact the title is almost the same!) except that my case is a generalization. I want to divide a 2D array into a sub-bl

Solution 1:

You can do:

rows, cols = a.shape

# sample data
a=np.arange(24).reshape((6,4))

a.reshape(rows//2, 2, cols//2, 2).mean(axis=(1,-1))

Output:

array([[ 2.5,  4.5],
       [10.5, 12.5],
       [18.5, 20.5]])

Post a Comment for "Block Mean Of 2d Numpy Array (in Both Dimensions)"