NumPy expand dims adds a length one axis without adding new values

A list of three numbers can be stored in several array shapes. The numbers may stay the same while the structure around them changes. Academia Master Trader explains NumPy's expand dims operation in English for readers in Finland. We will start with a one dimensional array and follow three ways to insert new axes. The aim is to read the result from its shape, rather than assume that an extra dimension means extra observations. These examples describe data structure, not financial outcomes.

Record the starting array

After import numpy as np, define a = np.array([8,3,6]). Its shape is (3,). This means that the array has one axis with three positions. The comma is part of the tuple notation for a single entry. It does not indicate an empty second axis. Our input contains three elements, and its number of dimensions is one.

The function is written as np.expand_dims in code. It inserts an axis of length one at a chosen position in the expanded shape. That position is supplied through the axis argument. The operation does not invent a second observation for any of the numbers. It changes the way the same elements are arranged within axes. Keep the element count separate from the dimension count as you read each result.

Insert an axis before the existing axis

The call np.expand_dims(a,axis=0) produces [[8,3,6]] with shape (1,3). There are now two axes. The new first axis has length one, and the original three position axis comes after it. In an ordinary two dimensional table description, this is one row containing three values. The values remain eight, three and six in that order.

Notice that zero names a position in the result's axis order. It is not a request to create an axis of length zero. The inserted length is one. Reading axis as a length would lead to the wrong explanation of the array. Write the starting shape and the expanded shape beside the call so that the change can be checked directly.

Insert an axis after the existing axis

The call np.expand_dims(a,axis=1) produces [[8],[3],[6]] with shape (3,1). Here the original three position axis comes first and the inserted one position axis comes second. A table description is three rows with one value in each row. This result and the previous result contain the same number of elements, but they have different shapes.

Those shapes matter to later operations that match or combine arrays along axes. Do not treat (1,3) and (3,1) as interchangeable simply because their printed values look familiar. The position of the length one axis is the important difference. Expanding a shape is therefore a structural choice, not a calculation that makes the numbers larger.

NumPy expand dims

Read the figure through its shapes

The figure starts with shape (3,) and the values eight, three and six. Each row then pairs a function call with its resulting shape. The first row inserts an axis at position zero. The second inserts it at position one. The third inserts two axes in one call. Read each row independently from the same starting array, rather than treating the rows as consecutive changes.

For np.expand_dims(a,axis=(0,1)), the shape is (1,1,3). The new axes occupy positions zero and one in the expanded shape. The original axis of length three follows them. The array now has three dimensions and still contains three elements. The tuple identifies the positions of the inserted axes, not a pair of desired lengths for the whole result.

Separate a view from a new independent copy

The NumPy documentation describes the result as a view of the input with the number of dimensions increased. It should therefore not be presented as an independent copy of the values. A change of shape and a separate allocation of data are different ideas. If a later step will change elements, the relationship with the original data also needs attention.

This distinction is easy to miss when an array is displayed with extra brackets. Brackets show structure in the printed representation; they do not by themselves prove that new independent values were created. In our examples the element count stays three. The shape tells us where the added axes sit, while the documentation tells us how to describe the result's relationship to the input.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Start with the original shape, mark the inserted axis positions and write the final shape. Then read the values separately. This small habit keeps a new dimension from being confused with a new observation and makes the role of axis easier to explain.