Instagram
youtube
Facebook
Twitter

Reshape 3D NumPy Array to 2D Array

A Reshape 3D NumPy Array to 2D Array?
Code Explanation:
Library Import:
numpy is imported as np for handling arrays and reshaping.
Create 3D Array: np.random.rand(2, 3, 4) creates a 3D array with shape (2, 3, 4) filled with random values.
Reshape Operation: .reshape(6, 4) transforms the 3D array into a 2D array with 6 rows and 4 columns.
Size Match: The total number of elements remains the same (2×3×4 = 24), ensuring the reshape is valid.
Store Result: The reshaped array is stored in the variable reshaped.
Display Output: print(reshaped) displays the 2D version of the array.

 

Program:

import numpy as np

arr = np.random.rand(2, 3, 4)

reshaped = arr.reshape(6, 4)

print(reshaped)