0
0
NumPydata~5 mins

Why reshaping arrays matters in NumPy

Choose your learning style9 modes available
Introduction

Reshaping arrays helps us change the shape of data without changing its content. This is useful to prepare data for analysis or visualization.

When you get data in a flat list but need it in rows and columns to see patterns.
When you want to combine or split data into different groups for comparison.
When a machine learning model expects data in a specific shape.
When you want to convert a multi-dimensional array into a single list for simple calculations.
When you need to match shapes of arrays to perform math operations between them.
Syntax
NumPy
import numpy as np

# Reshape an array
reshaped_array = original_array.reshape(new_shape)

The total number of elements must stay the same before and after reshaping.

Use -1 in new_shape to let numpy calculate the correct size automatically for that dimension.

Examples
Reshape a 1D array of 6 elements into a 2x3 2D array.
NumPy
import numpy as np

# Original array with 6 elements
original_array = np.array([1, 2, 3, 4, 5, 6])

# Reshape to 2 rows and 3 columns
reshaped_array = original_array.reshape(2, 3)
print(reshaped_array)
Use -1 to let numpy figure out the number of columns (here 2 columns).
NumPy
import numpy as np

# Original array with 6 elements
original_array = np.array([1, 2, 3, 4, 5, 6])

# Use -1 to automatically calculate columns
reshaped_array = original_array.reshape(3, -1)
print(reshaped_array)
Reshaping an empty array to have zero rows and 3 columns results in an empty 2D array with shape (0, 3).
NumPy
import numpy as np

# Empty array
empty_array = np.array([])

# Reshape empty array to (0, 3)
reshaped_empty = empty_array.reshape(0, 3)
print(reshaped_empty)
Reshaping a single element array to a 2D array with one row and one column.
NumPy
import numpy as np

# Single element array
single_element_array = np.array([42])

# Reshape to (1, 1)
reshaped_single = single_element_array.reshape(1, 1)
print(reshaped_single)
Sample Program

This program shows how to reshape a 1D array into different 2D shapes. It prints the original array and its shape, then reshapes it to 3 rows and 4 columns, and finally reshapes it to 2 rows with automatic column calculation.

NumPy
import numpy as np

# Create a 1D array with 12 elements
original_array = np.arange(1, 13)
print("Original array:")
print(original_array)
print("Shape:", original_array.shape)

# Reshape to 3 rows and 4 columns
reshaped_array = original_array.reshape(3, 4)
print("\nReshaped array (3x4):")
print(reshaped_array)
print("Shape:", reshaped_array.shape)

# Reshape to 2 rows and let numpy calculate columns
reshaped_auto = original_array.reshape(2, -1)
print("\nReshaped array (2x?):")
print(reshaped_auto)
print("Shape:", reshaped_auto.shape)
OutputSuccess
Important Notes

Reshaping does not change the data, only how it is viewed.

Time complexity is O(1) because no data copying happens, just a new view.

Common mistake: Trying to reshape to a shape with a different total number of elements causes an error.

Use reshaping to prepare data for functions that expect specific input shapes.

Summary

Reshaping changes the shape of data without changing its content.

Use -1 to let numpy calculate one dimension automatically.

Always ensure total elements stay the same before and after reshaping.