0
0
NumPydata~10 mins

np.expand_dims() and np.squeeze() in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.expand_dims() and np.squeeze()
Start with array
Apply np.expand_dims(axis)
Array shape increases by 1 dimension
Apply np.squeeze()
Remove all size 1 dimensions
Resulting array with changed shape
Start with an array, add a new axis with np.expand_dims(), then remove size 1 axes with np.squeeze().
Execution Sample
NumPy
import numpy as np
arr = np.array([1, 2, 3])
expanded = np.expand_dims(arr, axis=0)
squeezed = np.squeeze(expanded)
Create 1D array, add a new axis at front, then remove all single-dimensional axes.
Execution Table
StepArray ShapeOperationResulting ShapeExplanation
1(3,)Start with arr(3,)Original 1D array with 3 elements
2(3,)np.expand_dims(arr, axis=0)(1, 3)Add new axis at front, shape becomes 2D
3(1, 3)np.squeeze(expanded)(3,)Remove size 1 axis, back to 1D array
💡 All operations complete, final array shape is (3,)
Variable Tracker
VariableStartAfter np.expand_dimsAfter np.squeeze
arr.shape(3,)(3,)(3,)
expanded.shapeN/A(1, 3)(1, 3)
squeezed.shapeN/AN/A(3,)
Key Moments - 3 Insights
Why does np.expand_dims increase the number of dimensions?
Because np.expand_dims inserts a new axis at the specified position, increasing the array's rank by 1 as shown in execution_table step 2.
Does np.squeeze remove all axes or only those with size 1?
np.squeeze only removes axes with size 1, leaving other dimensions unchanged, as seen in execution_table step 3.
What happens if np.squeeze is applied to an array with no size 1 axes?
The array shape stays the same because there are no size 1 axes to remove, so no change occurs.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the shape of 'expanded' after np.expand_dims at step 2?
A(3,)
B(1, 3)
C(3, 1)
D(1,)
💡 Hint
Check the 'Resulting Shape' column at step 2 in the execution_table.
At which step does the array shape return to (3,)?
AStep 3
BStep 2
CStep 1
DNever
💡 Hint
Look at the 'Resulting Shape' column and find when shape is (3,).
If np.expand_dims(arr, axis=1) was used instead of axis=0, what would be the shape after expansion?
A(1, 3)
B(3,)
C(3, 1)
D(1,)
💡 Hint
Adding axis=1 inserts a new dimension after the first dimension, changing shape accordingly.
Concept Snapshot
np.expand_dims(array, axis) adds a new axis at 'axis', increasing dimensions by 1.
np.squeeze(array) removes all axes of size 1.
Use expand_dims to prepare arrays for broadcasting or model input.
Use squeeze to remove unnecessary single dimensions.
Shapes change accordingly: expand_dims adds, squeeze removes size 1 axes.
Full Transcript
We start with a 1D numpy array of shape (3,). Using np.expand_dims with axis=0 adds a new dimension at the front, changing the shape to (1, 3). Then, applying np.squeeze removes all dimensions of size 1, returning the shape back to (3,). This process helps reshape arrays for different operations by adding or removing single-dimensional axes.