w3resource

Creating and reshaping a 4D NumPy array

NumPy: Memory Layout Exercise-17 with Solution

Write a NumPy program that creates a 4D array of shape (2, 2, 3, 3), reshape it into a 2D array, and then reshape it back to the original shape. Print all the intermediate arrays.

Sample Solution:

Python Code:

import numpy as np

# Step 1: Create a 4D array of shape (2, 2, 3, 3)
original_array = np.random.rand(2, 2, 3, 3)
print("Original 4D array:\n", original_array)

# Step 2: Reshape the 4D array into a 2D array
reshaped_2d_array = original_array.reshape(-1, 9)
print("\nReshaped 2D array:\n", reshaped_2d_array)

# Step 3: Reshape the 2D array back to the original 4D shape
reshaped_back_to_4d = reshaped_2d_array.reshape(2, 2, 3, 3)
print("\nReshaped back to 4D array:\n", reshaped_back_to_4d)

Output:

Original 4D array:
 [[[[0.87825329 0.92814202 0.72976418]
   [0.85342904 0.1590352  0.36378863]
   [0.6212089  0.2612585  0.70702319]]

  [[0.51610167 0.46228402 0.84994689]
   [0.70451938 0.01329465 0.86950388]
   [0.32117702 0.92568944 0.86945406]]]


 [[[0.90918915 0.41516567 0.77052305]
   [0.34892452 0.11246739 0.72093766]
   [0.22318908 0.00657031 0.06388555]]

  [[0.61764261 0.84885538 0.49016637]
   [0.46874106 0.90037212 0.34975796]
   [0.63524874 0.59394007 0.12072371]]]]

Reshaped 2D array:
 [[0.87825329 0.92814202 0.72976418 0.85342904 0.1590352  0.36378863
  0.6212089  0.2612585  0.70702319]
 [0.51610167 0.46228402 0.84994689 0.70451938 0.01329465 0.86950388
  0.32117702 0.92568944 0.86945406]
 [0.90918915 0.41516567 0.77052305 0.34892452 0.11246739 0.72093766
  0.22318908 0.00657031 0.06388555]
 [0.61764261 0.84885538 0.49016637 0.46874106 0.90037212 0.34975796
  0.63524874 0.59394007 0.12072371]]

Reshaped back to 4D array:
 [[[[0.87825329 0.92814202 0.72976418]
   [0.85342904 0.1590352  0.36378863]
   [0.6212089  0.2612585  0.70702319]]

  [[0.51610167 0.46228402 0.84994689]
   [0.70451938 0.01329465 0.86950388]
   [0.32117702 0.92568944 0.86945406]]]


 [[[0.90918915 0.41516567 0.77052305]
   [0.34892452 0.11246739 0.72093766]
   [0.22318908 0.00657031 0.06388555]]

  [[0.61764261 0.84885538 0.49016637]
   [0.46874106 0.90037212 0.34975796]
   [0.63524874 0.59394007 0.12072371]]]]

Explanation:

  • Create a 4D array: A 4D array of shape (2, 2, 3, 3) is created using np.random.rand().
  • Reshape to 2D: The 4D array is reshaped into a 2D array with shape (-1, 9), flattening the first three dimensions into a single dimension.
  • Reshape back to 4D: The 2D array is reshaped back to the original 4D shape (2, 2, 3, 3).

Python-Numpy Code Editor:

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

Previous: How to slice a Sub-array from a reshaped 1D NumPy array and print strides?
Next: Creating and reshaping a 2D NumPy array.

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://w3resource.com/python-exercises/numpy/creating-and-reshaping-a-4d-numpy-array.php