w3resource

NumPy: Get the block-sum from a given array of shape 25x25


Compute block-sum of a 25x25 matrix (e.g., sum of each 5x5 block).

Write a NumPy program to get the block-sum (block size is 5x5) from a given array of shape 25x25.

Pictorial Presentation:

Python NumPy: Get the block-sum from a given array of shape 25x25

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating a NumPy array 'arra1' of shape (25, 25) filled with ones
arra1 = np.ones((25, 25))

# Setting the block size 'k' as 5
k = 5

# Displaying the original array 'arra1'
print("Original arrays:")
print(arra1)

# Calculating the block-sum (5x5) of the original array 'arra1'
result = np.add.reduceat(
    np.add.reduceat(arra1, np.arange(0, arra1.shape[0], k), axis=0),
    np.arange(0, arra1.shape[1], k), axis=1
)

# Displaying the block-sum result
print("\nBlock-sum (5x5) of the said array:")
print(result)

Sample Output:

Original arrays:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.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1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]

Block-sum (5x5) of the said array:
[[25. 25. 25. 25. 25.]
 [25. 25. 25. 25. 25.]
 [25. 25. 25. 25. 25.]
 [25. 25. 25. 25. 25.]
 [25. 25. 25. 25. 25.]]

Explanation:

In the above exercise -

arra1: A 2D NumPy array of shape (25, 25) filled with ones.

k: The size of the non-overlapping blocks to calculate the sum.

result = np.add.reduceat(np.add.reduceat(arra1, np.arange(0, arra1.shape[0], k), axis=0), np.arange(0, arra1.shape[1], k), axis=1)

In the above code -

  • np.add.reduceat: This function performs a local reduce (in this case, addition) on the input array along the specified axis using the given indices.
  • np.arange(0, arra1.shape[0], k): This creates an array of indices with a step of k for the rows. It will be [0, 5, 10, 15, 20].
  • np.arange(0, arra1.shape[1], k): This creates an array of indices with a step of k for the columns. It will be [0, 5, 10, 15, 20].
  • The first np.add.reduceat is applied to the rows, resulting in an intermediate array with summed blocks of size kxk along the rows.
  • The second np.add.reduceat is applied to the columns of the intermediate array, resulting in the final array result, which has the summed blocks of size kxk.

print(result): Finally print() function prints the final result.


For more Practice: Solve these Related Problems:

  • Write a NumPy program to divide a 25x25 matrix into non-overlapping 5x5 blocks and compute the sum of each block using np.add.reduceat.
  • Create a function that reshapes the matrix into a 5x5 grid of blocks and then computes the block-sum for each.
  • Implement a solution that uses slicing and loops to manually compute the sum of each 5x5 block.
  • Test the block-sum function on matrices with different block sizes to verify its generality.

Go to:


PREV : Create a record array from a regular array.
NEXT : Extract all contiguous 4x4 blocks from a 12x12 matrix.


Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.