Tensor Operations Worth Mastering: reshape(N, -1)
NumPy reshape: Taking Full Control of Your Data's Shape
NumPy's reshape function is a powerful tool for changing the shape of multidimensional arrays. In this article, we cover reshape in detail, from basic usage to more advanced applications, with concrete examples.
1. reshape Basics
reshape changes an array's shape without changing the number of elements.
import numpy as np
# Create a 1-D array
arr = np.array([1, 2, 3, 4, 5, 6])
print("Original array:", arr)
print("Shape:", arr.shape)
# Reshape into a 2x3 2-D array
reshaped = arr.reshape(2, 3)
print("\nReshaped to 2x3:")
print(reshaped)
print("New shape:", reshaped.shape)
# Output:
# Original array: [1 2 3 4 5 6]
# Shape: (6,)
#
# Reshaped to 2x3:
# [[1 2 3]
# [4 5 6]]
# New shape: (2, 3)
2. Using "-1"
-1 in a dimension slot tells NumPy to compute that dimension's size automatically.
# Reshape into a 3x2 2-D array
reshaped_auto = arr.reshape(3, -1)
print("Reshaped to 3x2:")
print(reshaped_auto)
print("Shape:", reshaped_auto.shape)
# Output:
# Reshaped to 3x2:
# [[1 2]
# [3 4]
# [5 6]]
# Shape: (3, 2)
3. Reshaping Multidimensional Arrays
Multidimensional arrays can be reshaped just as easily.
# Create a 3-D array
arr_3d = np.arange(24).reshape(2, 3, 4)
print("3D array:")
print(arr_3d)
print("Shape:", arr_3d.shape)
# Reshape into a 4x6 2-D array
reshaped_2d = arr_3d.reshape(4, 6)
print("\nReshaped to 4x6:")
print(reshaped_2d)
print("New shape:", reshaped_2d.shape)
# Output:
# 3D array:
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
# Shape: (2, 3, 4)
#
# Reshaped to 4x6:
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
# New shape: (4, 6)
4. Loading .mat Files and reshape
In our model engineering work, we frequently use MATLAB data. MATLAB-format data comes as mat files (.mat files), a highly versatile data format.
.matLet's look at an example of adjusting the shape of data loaded from a file.
from scipy.io import loadmat
# Load the .mat file (assuming the file exists)
mat_data = loadmat('example.mat')
data = mat_data['some_key']
print("Original shape:", data.shape)
# Reshape to the expected shape
expected_shape = (273, 260)
reshaped_data = data.reshape(expected_shape)
print("Reshaped data shape:", reshaped_data.shape)
# Output:
# Original shape: (70980,)
# Reshaped data shape: (273, 260)
In this example, even if the data loaded with loadmat has been squeezed down to one dimension, reshape can restore it to its original two-dimensional shape.
5. A Safe reshape, Squeezed or Not
.matEven when your data has been squeezed (has lost dimensions) as a result of file loading or other processing, reshape can safely transform it to the intended size. The example below shows how to apply the same reshape operation to both squeezed data and data in its original shape.
import numpy as np
from scipy.io import savemat, loadmat
# Original data (2D)
original_data = np.arange(24).reshape(4, 6)
print("Original data shape:", original_data.shape)
# Save to a .mat file
savemat('test_data.mat', {'data': original_data})
# 1. Squeezed case (1D)
squeezed_data = loadmat('test_data.mat')['data'].squeeze()
print("Squeezed data shape:", squeezed_data.shape)
# 2. Loaded as 2D
normal_data = loadmat('test_data.mat')['data']
print("Normal loaded data shape:", normal_data.shape)
# Apply the same reshape operation to both cases
target_shape = (4, 6)
reshaped_squeezed = squeezed_data.reshape(target_shape)
reshaped_normal = normal_data.reshape(target_shape)
print("Reshaped from squeezed shape:", reshaped_squeezed.shape)
print("Reshaped from normal shape:", reshaped_normal.shape)
# Check whether the results match the original data
print("Squeezed data reshaped correctly:", np.array_equal(original_data, reshaped_squeezed))
print("Normal data reshaped correctly:", np.array_equal(original_data, reshaped_normal))
# Output:
# Original data shape: (4, 6)
# Squeezed data shape: (24,)
# Normal loaded data shape: (4, 6)
# Reshaped from squeezed shape: (4, 6)
# Reshaped from normal shape: (4, 6)
# Squeezed data reshaped correctly: True
# Normal data reshaped correctly: True
As this example shows, the reshape operation is remarkably flexible:
- For squeezed (one-dimensional) data,
- and for data in its original two-dimensional shape,
the same reshape(4, 6) operation transforms both into the desired shape.
Why This Works
- Preservation of element count:
reshapenever changes the total number of elements in an array. As long as the total element count matches, you can reshape to any shape. - Memory layout: Internally, NumPy stores data as a one-dimensional array; a multidimensional array is essentially a "view" onto this flat array. That is why shapes can be changed flexibly, regardless of the number of dimensions.
- Preservation of order:
reshapepreserves the element order of the original array. Even after squeezing, the original order is retained, which is why the shape can be restored correctly.
Caveats
- This all assumes the total element counts match. If they do not, a
ValueErroris raised. - For very large arrays there can be performance implications, so consider optimizing where necessary.
By adopting this approach, you can always shape your data into the expected form regardless of how it was loaded or preprocessed. This stabilizes downstream processing and keeps the data consistent without affecting other parts of your code.
Summary
reshape is one of NumPy's most convenient and powerful functions. It comes into play in many situations: data preprocessing, preparing inputs for machine learning models, data visualization, and more. Used correctly, it makes even complex data structures easy to manipulate.
Keep a few points in mind, and reshape is not hard to master:
- The element count of the original array must match that of the new shape.
-1lets you have the size of one dimension computed automatically.- Multidimensional arrays can be reshaped just as easily.
- Whether the data has been squeezed or is still in its original shape, the same
reshapeoperation transforms it into the desired shape. .matIt is also handy when converting between data formats, such as when loading files.- Combined with other NumPy operations, it enables even more flexible data manipulation.
reshape will make your data analysis and machine learning workflows run more smoothly. In particular, it is worth remembering that even when the shape of your data is uncertain, reshape can safely transform it into the desired shape — something that will serve you well in many situations.
Appendix: Combining reshape with Other Operations
reshape can also be used in combination with other NumPy operations.
# Combine with transpose
transposed = arr.reshape(2, 3).T
print("Reshaped and transposed:")
print(transposed)
print("Shape:", transposed.shape)
# Combine with flattening
flattened = arr_3d.reshape(-1)
print("\nFlattened 3D array:")
print(flattened)
print("Shape:", flattened.shape)
# Output:
# Reshaped and transposed:
# [[1 4]
# [2 5]
# [3 6]]
# Shape: (3, 2)
#
# Flattened 3D array:
# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
# Shape: (24,)