NumPy expand dims adds length-one axes while keeping the original entries
A vector can represent one row, one column or data awaiting an extra grouping direction. NumPy expand dims adds explicit length-one axes so these structural choices appear in the shape. Academia Master Trader presents this English lesson for readers in Equatorial Guinea, using small constructed arrays to separate axis insertion from value repetition.
One vector can acquire different orientations
After import numpy as np, define x = np.array([3,6,9]). Its shape is (3,), which describes one axis containing three positions. A one-dimensional vector has no separate row and column axes.
The call row = np.expand_dims(x,axis=0) gives shape (1,3) and displays [[3,6,9]]. The new first axis has length one. The existing length-three axis moves to position one.
The call column = np.expand_dims(x,axis=1) gives shape (3,1) and displays [[3],[6],[9]]. This time the new singleton axis follows the existing axis. The same three values now have a column-shaped arrangement.
Neither operation adds numerical entries. Both results contain three elements, even though their shapes have two components rather than one.

Axis positions describe the expanded shape
The axis argument identifies where a new axis appears in the output shape. For this vector, axis zero inserts before its current axis, while axis one inserts after it.
A negative axis can express a position counted from the end. The call np.expand_dims(x,axis=-1) produces shape (3,1), matching the column-shaped result above.
Keep axis positions distinct from axis lengths. Inserting at axis zero creates a dimension of length one; it does not create a dimension of length zero or remove the first value.
For several new axes, pass a tuple. The call np.expand_dims(x,axis=(0,2)) produces shape (1,3,1). The singleton axes occupy positions zero and two in the expanded result, with the original length-three axis between them.
Extra axes are not repeated data
Adding a singleton direction can make an array's intended alignment explicit before broadcasting. Expansion itself does not fill that direction with repeated copies, and it does not calculate a sum or average.
Before combining arrays, write their shapes in order. A row-shaped (1,3) array and a column-shaped (3,1) array align differently, even when their flattened values match.
This distinction matters when later expressions use shape compatibility. An extra axis is a structural instruction for alignment, not evidence that more observations have been collected.
The result is a view
NumPy documents the returned result as a view of the input with more dimensions. Do not assume that inserting an axis allocates independent editable values.
For separate data storage, use an explicit copy, such as separate = np.expand_dims(x,axis=0).copy(). Removing a known length-one axis with squeeze can reverse the corresponding shape change, but does not itself promise a copy either.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the original shape, insertion positions and resulting shape before relying on an expanded array.