# Fetching Block-Sum of an Array

In NumPy, you can easily calculate the block-sum of a two-dimensional array by dividing it into non-overlapping blocks and computing the sum of each block. This is useful in many image processing and computer vision applications where you need to analyze an image by breaking it down into smaller blocks. In this article, we will explore how to get a block-sum of an array using two different NumPy methods.

• In the following examples, we will suppose that the size of the block-sum must be (4x4)

#### 1. Using “np.reshape()” and “np.sum()”:

This code creates a 2D array `Z` of size 16x16, and then divides it into non-overlapping blocks of size 4x4 using the `np.reshape()` function. The blocks are stored in a new array called `blocks`. The `np.sum()` function is then used to sum over the first and third axes of `blocks` to obtain the sum of each 4x4 block, which is stored in a new 2D array called `S`. The code essentially computes the block-sum of `Z`, where each element of `S` represents the sum of a 4x4 block in `Z`.

#### 2. Using “np.lib.stride_tricks.as_strided()” and “np.sum()”:

This code generates a 2D array `Z` of size 16x16, and then divides it into non-overlapping blocks of size 4x4 using the `np.lib.stride_tricks.as_strided()` function. The blocks are stored in a new array called `block_view`. The `np.sum()` function is then used to sum over the last two axes of `block_view` to obtain the sum of each 4x4 block, which is stored in a new 2D array called `S`. The code essentially computes the block-sum of `Z`, where each element of `S` represents the sum of a 4x4 block in `Z`.