Adding a singleton axis in NumPy changes dimensional structure without adding entries

A one dimensional array and a matrix can contain the same values while having different shapes. Some calculations require an explicit row, column or extra grouping dimension. NumPy provides a function that inserts an axis of length one at a chosen position. Academia Master Trader presents this English lesson for readers in Taiwan, using constructed examples to distinguish a new dimension from new observations and to make the resulting shapes clear.

Begin with a one dimensional array

After import numpy as np, define v = np.array([3,6,9]). Its shape is (3,), so it has one dimension containing three entries. The call r = np.expand_dims(v,axis=0) gives [[3,6,9]], with shape (1,3). The new leading axis has length one.

The entries remain three, six and nine. A second dimension has appeared, but no additional numeric entries have been recorded. The output can be described as one row containing three columns. That description depends on the two dimensional shape, not merely on the values printed inside the array.

NumPy singleton axes

Place the new axis after the original one

The call c = np.expand_dims(v,axis=1) gives [[3],[6],[9]], with shape (3,1). There are now three rows and one column. The same source vector therefore supports two different two dimensional arrangements, depending on where the singleton axis is inserted.

For this source, np.expand_dims(v,axis=-1) also gives shape (3,1). Negative one names the final position in the expanded shape. The axis argument describes the position of the new dimension, not the position of an entry that should be duplicated or moved.

Keep dimension count separate from entry count

The original v has one dimension; r and c each have two. All three arrays contain three entries. Inserting a singleton axis increases dimensionality without increasing the product of the axis lengths. Multiplying by a new length of one leaves that product unchanged.

This distinction is useful when preparing arrays for an operation that expects explicit dimensions. State what each axis means instead of assuming that one extra axis represents an extra measurement. The function changes structure; it does not collect data or manufacture a missing observation.

Several new axes can be requested together

The call np.expand_dims(v,axis=(0,2)) gives shape (1,3,1). The newly inserted dimensions occupy positions zero and two in the expanded result. The original length three dimension sits between them at position one.

The result is [[[3],[6],[9]]]. Reading its shape alongside the brackets makes the arrangement easier to follow. Both new dimensions have length one, and the number of numeric entries is still three. Use an explicit description of the output axes when more than one new position is involved.

A new shape can still share source data

The documentation describes the result as a view with an increased number of dimensions. Starting again from v, set r = np.expand_dims(v,axis=0). Writing r[0,1] = 99 also changes v[1] to ninety nine. The extra dimension does not make later edits independent of the source.

Use an explicit copy when separate editing is required. The call np.squeeze(r,axis=0) removes this length one axis and returns the original one dimensional shape. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the starting shape, inserted axis positions and final shape, keeping the number of dimensions distinct from the number of entries.