Reshape a NumPy array in Python using the reshape() function. This function allows you to change the shape (dimensions) of the array while keeping the same data. Here’s a basic example:
Code:
import numpy as np
#Here we are Creating a NumPy array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Reshape the array to have 3 rows and 2 columns
reshaped_arr = arr.reshape(3, 2)
print("Original array:")
print(arr)
print("Reshaped array:")
print(reshaped_arr)
Output:
Original array: [[1 2 3] [4 5 6]] Reshaped array: [[1 2] [3 4] [5 6]]