Reshaping Arrays in NumPy
In NumPy, reshaping means changing the shape of an existing array without changing its data. The .reshape() method allows you to transform an array’s dimensions—like turning a 1D array into a 2D matrix or a 2D array into a 3D block.
Reshaping is especially useful in data science and machine learning workflows where your data must be organized in a specific shape for analysis or model training.
- 1D → 2D: Turn a flat array into rows and columns.
- 2D → 3D: Reshape a matrix into multi-layered structures.
- Flexible Dimensions: Use -1 to auto-calculate dimensions.
With NumPy's flexible reshaping tools, you can easily adapt array structures to match your processing needs—while keeping all of the original data intact.
Reshaping Arrays
In NumPy, the reshape() function allows you to change the shape of an array without changing its data. This is especially useful when preparing data for matrix operations, machine learning models, or image processing tasks.
For example, you can take a 1D array and reshape it into a 2D array with rows and columns.
import numpy as np
# Create a 1D array with 6 elements
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshape into a 2D array (2 rows, 3 columns)
reshaped = arr.reshape(2, 3)
print(reshaped)
import numpy as np
# Create a 1D array with 6 elements
arr = np.array([1, 2, 3, 4, 5, 6])
# Reshape into a 2D array (2 rows, 3 columns)
reshaped = arr.reshape(2, 3)
print(reshaped)
How It Works:
- arr is a 1D array with 6 elements.
- reshape(2, 3) creates a new view of the array with 2 rows and 3 columns.
- No data is lost — just reorganized.
- The product of the dimensions in the new shape (2 × 3 = 6) must match the original number of elements in the array.
Output
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]
You can reshape arrays into any shape, as long as the total number of elements stays the same. For example, the array above (6 elements) could also be reshaped into a 3x2 or 6x1 array.
print(arr.reshape(3, 2)) # 3 rows, 2 columns
print(arr.reshape(6, 1)) # 6 rows, 1 column
print(arr.reshape(3, 2)) # 3 rows, 2 columns
print(arr.reshape(6, 1)) # 6 rows, 1 column
If the reshape isn’t possible (for example, if the new shape doesn’t match the number of elements), NumPy will raise an error.
💡 Tip: You can use -1 to let NumPy automatically calculate one dimension for you:
arr.reshape(1, -1) # NumPy will infer the correct number of columns
arr.reshape(1, -1) # NumPy will infer the correct number of columns
This is handy when you know how many rows (or columns) you want, but not the other dimension.
Example 1: Reshape a 3D Array to 2D
In this example, we'll take a 3D array and reshape it into a 2D array. This is useful when flattening grouped data into a tabular format for easier processing or analysis. We'll use the .reshape() method to achieve this.
import numpy as np
# Create a 3D array with shape (2, 3, 2)
arr = np.array([
[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]]
])
# Reshape it into a 2D array
reshaped = arr.reshape(6, 2)
print("Original shape:", arr.shape)
print("Reshaped shape:", reshaped.shape)
print("Reshaped array:")
print(reshaped)
import numpy as np
# Create a 3D array with shape (2, 3, 2)
arr = np.array([
[[1, 2], [3, 4], [5, 6]],
[[7, 8], [9, 10], [11, 12]]
])
# Reshape it into a 2D array
reshaped = arr.reshape(6, 2)
print("Original shape:", arr.shape)
print("Reshaped shape:", reshaped.shape)
print("Reshaped array:")
print(reshaped)
How It Works:
- arr is a 3D array with shape (2, 3, 2), meaning 2 blocks of 3 rows and 2 columns.
- arr.reshape(6, 2) flattens it into a 2D array with 6 rows and 2 columns.
- The total number of elements remains constant: 2 × 3 × 2 = 12.
Output
Original shape: (2, 3, 2)
Reshaped shape: (6, 2)
Reshaped array:
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
Original shape: (2, 3, 2)
Reshaped shape: (6, 2)
Reshaped array:
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
💡 Tip: When reshaping from higher to lower dimensions, think of it as “unstacking” the nested structure into a flat, row-wise layout.
Frequently Asked Questions
What does reshaping an array mean in NumPy?
What does reshaping an array mean in NumPy?
Reshaping means changing the shape or dimensions of an array without modifying its data. NumPy's reshape() method is used for this.
Can I reshape an array to any shape?
Can I reshape an array to any shape?
You can reshape an array as long as the total number of elements remains the same. Otherwise, NumPy will raise an error.
What does using -1 in reshape() do?
What does using -1 in reshape() do?
Using -1 allows NumPy to automatically calculate the dimension size based on the array’s total elements and the other specified dimensions.
Does reshaping change the data in the array?
Does reshaping change the data in the array?
No, reshaping only changes how the data is viewed, not the actual data values.
Is the reshaped array a copy or a view?
Is the reshaped array a copy or a view?
The reshaped array is usually a view of the original data, but sometimes it might be a copy. Check your use case if you plan to modify it.
What's Next?
Up next, we’ll explore Axes in NumPy — an essential concept for understanding how operations like summing, reshaping, or broadcasting work across different dimensions of an array.