w3resource

NumPy: Extract all the contiguous 4x4 blocks from a given random 12x12 matrix

NumPy: Array Object Exercise-192 with Solution

Extract all contiguous 4x4 blocks from a 12x12 matrix.

Write a NumPy program to extract all the contiguous 4x4 blocks from a given random 12x12 matrix.

Sample Solution:

Python Code:

# Importing the NumPy library
import numpy as np

# Creating a random integer NumPy array 'arra1' of shape (12, 12) with values between 0 and 4
arra1 = np.random.randint(0, 5, (12, 12))

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

# Specifying the block size 'n' as 4
n = 4

# Calculating dimensions 'i' and 'j' for the resulting array based on the original array shape
i = 1 + (arra1.shape[0] - 4)
j = 1 + (arra1.shape[1] - 4)

# Using stride tricks to create a view on 'arra1' with shape (i, j, n, n) representing contiguous 4x4 blocks
result = np.lib.stride_tricks.as_strided(arra1, shape=(i, j, n, n), strides=arra1.strides + arra1.strides)

# Displaying the contiguous 4x4 blocks obtained from the original array
print("\nContiguous 4x4 blocks:")
print(result)

Sample Output:

Original arrays:
[[3 3 1 2 0 3 0 3 2 0 1 0]
 [4 0 4 2 0 0 0 0 2 2 2 3]
 [1 4 0 1 1 3 4 1 0 3 3 3]
 [1 4 2 4 2 0 0 4 2 1 2 3]
 [4 3 0 2 3 1 0 1 1 1 2 2]
 [1 0 1 1 4 4 3 1 4 2 3 4]
 [1 4 0 4 1 2 0 2 4 1 4 4]
 [2 2 0 4 1 4 2 4 1 4 0 3]
 [3 0 4 0 3 1 2 4 4 1 1 0]
 [3 1 3 3 0 0 4 3 1 0 2 2]
 [1 2 3 4 1 2 3 4 3 2 3 4]
 [4 0 4 2 2 4 1 4 2 0 0 0]]

Contiguous 4x4 blocks:
[[[[3 3 1 2]
   [4 0 4 2]
   [1 4 0 1]
   [1 4 2 4]]

  [[3 1 2 0]
   [0 4 2 0]
   [4 0 1 1]
   [4 2 4 2]]

  [[1 2 0 3]
   [4 2 0 0]
   [0 1 1 3]
   [2 4 2 0]]

  ...

  [[0 3 2 0]
   [0 0 2 2]
   [4 1 0 3]
   [0 4 2 1]]

  [[3 2 0 1]
   [0 2 2 2]
   [1 0 3 3]
   [4 2 1 2]]

  [[2 0 1 0]
   [2 2 2 3]
   [0 3 3 3]
   [2 1 2 3]]]


 [[[4 0 4 2]
   [1 4 0 1]
   [1 4 2 4]
   [4 3 0 2]]

  [[0 4 2 0]
   [4 0 1 1]
   [4 2 4 2]
   [3 0 2 3]]

  [[4 2 0 0]
   [0 1 1 3]
   [2 4 2 0]
   [0 2 3 1]]

  ...

  [[0 0 2 2]
   [4 1 0 3]
   [0 4 2 1]
   [0 1 1 1]]

  [[0 2 2 2]
   [1 0 3 3]
   [4 2 1 2]
   [1 1 1 2]]

  [[2 2 2 3]
   [0 3 3 3]
   [2 1 2 3]
   [1 1 2 2]]]


 [[[1 4 0 1]
   [1 4 2 4]
   [4 3 0 2]
   [1 0 1 1]]

  [[4 0 1 1]
   [4 2 4 2]
   [3 0 2 3]
   [0 1 1 4]]

  [[0 1 1 3]
   [2 4 2 0]
   [0 2 3 1]
   [1 1 4 4]]

  ...

  [[4 1 0 3]
   [0 4 2 1]
   [0 1 1 1]
   [3 1 4 2]]

  [[1 0 3 3]
   [4 2 1 2]
   [1 1 1 2]
   [1 4 2 3]]

  [[0 3 3 3]
   [2 1 2 3]
   [1 1 2 2]
   [4 2 3 4]]]


 ...


 [[[1 4 0 4]
   [2 2 0 4]
   [3 0 4 0]
   [3 1 3 3]]

  [[4 0 4 1]
   [2 0 4 1]
   [0 4 0 3]
   [1 3 3 0]]

  [[0 4 1 2]
   [0 4 1 4]
   [4 0 3 1]
   [3 3 0 0]]

  ...

  [[0 2 4 1]
   [2 4 1 4]
   [2 4 4 1]
   [4 3 1 0]]

  [[2 4 1 4]
   [4 1 4 0]
   [4 4 1 1]
   [3 1 0 2]]

  [[4 1 4 4]
   [1 4 0 3]
   [4 1 1 0]
   [1 0 2 2]]]


 [[[2 2 0 4]
   [3 0 4 0]
   [3 1 3 3]
   [1 2 3 4]]

  [[2 0 4 1]
   [0 4 0 3]
   [1 3 3 0]
   [2 3 4 1]]

  [[0 4 1 4]
   [4 0 3 1]
   [3 3 0 0]
   [3 4 1 2]]

  ...

  [[2 4 1 4]
   [2 4 4 1]
   [4 3 1 0]
   [3 4 3 2]]

  [[4 1 4 0]
   [4 4 1 1]
   [3 1 0 2]
   [4 3 2 3]]

  [[1 4 0 3]
   [4 1 1 0]
   [1 0 2 2]
   [3 2 3 4]]]


 [[[3 0 4 0]
   [3 1 3 3]
   [1 2 3 4]
   [4 0 4 2]]

  [[0 4 0 3]
   [1 3 3 0]
   [2 3 4 1]
   [0 4 2 2]]

  [[4 0 3 1]
   [3 3 0 0]
   [3 4 1 2]
   [4 2 2 4]]

  ...

  [[2 4 4 1]
   [4 3 1 0]
   [3 4 3 2]
   [1 4 2 0]]

  [[4 4 1 1]
   [3 1 0 2]
   [4 3 2 3]
   [4 2 0 0]]

  [[4 1 1 0]
   [1 0 2 2]
   [3 2 3 4]
   [2 0 0 0]]]]

Explanation:

In the above exercise -

arra1 = np.random.randint(0,5,(12,12)): This line creates a 2D NumPy array of shape (12, 12) with random integers between 0 (inclusive) and 5 (exclusive).

n: The size of the sliding window (in this case, 4x4).

i: The number of sliding window positions along the rows, calculated as 1 + (arra1.shape[0] - n).

j: The number of sliding window positions along the columns, calculated as 1 + (arra1.shape[1] - n).

result = np.lib.stride_tricks.as_strided(arra1, shape=(i, j, n, n), strides = arra1.strides + arra1.strides)

In the above code -

  • np.lib.stride_tricks.as_strided: This function creates a view of the input array with the specified shape and strides. It allows for creating the "sliding window" view without actually copying the data.
  • shape=(i, j, n, n): The shape of the output array, which consists of i positions along the rows, j positions along the columns, and nxn windows.
  • strides = arra1.strides + arra1.strides: The strides of the output array, calculated by concatenating the strides of the input array arra1. This ensures that the window slides by one element along both the rows and the columns.
  • result: The resulting 4D NumPy array that contains the sliding window view of arra1.

print(result): Finally print() function prints the resulting 4D NumPy array.

Python-Numpy Code Editor:

Previous: Write a NumPy program to get the block-sum (block size is 5x5) from a given array of shape 25x25.
Next: Write a Numpy program to test whether numpy array is faster than Python list or not.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/numpy/python-numpy-exercise-192.php