w3resource

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

NumPy: Array Object Exercise-191 with Solution

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. 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.]]

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.

Python-Numpy Code Editor:

Previous:Write a NumPy program to create a record array from a given regular array.
Next: Write a NumPy program to extract all the contiguous 4x4 blocks from a given random 12x12 matrix.

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.