Instagram
youtube
Facebook
Twitter

Reshape a NumPy Array from Shape (3, 4) to Shape (4, 3)

Reshape a NumPy Array from Shape (3, 4) to Shape (4, 3)
     
Short Description of the Program:

This program creates a NumPy array of shape (3, 4) and then reshapes it to (4, 3) using the .reshape() function.

Explanation:

  • np.arange(12)
    Generates a 1D array with 12 values: [0, 1, 2, ..., 11].

     
  • .reshape(3, 4)
    Converts the 1D array into a 3x4 matrix.

     
  • .reshape(4, 3)
    Reshapes the 3x4 matrix into a 4x3 matrix, keeping the total number of elements (12) the same.

     
  • print()
    Prints both the original and reshaped arrays for comparison.

Program:

import numpy as np

original_array = np.arange(12).reshape(3, 4)

reshaped_array = original_array.reshape(4, 3)

print("Original Array (3x4):\n", original_array)

print("\nReshaped Array (4x3):\n", reshaped_array)