NumPy: A new position in the shape

academiamastertrader.com.br offers learning material from Academia Master Trader. For English-speaking readers in the British Virgin Islands, from Road Town to Spanish Town, we present this example: the expand_dims function in NumPy. It adds a new position to the shape of a list, turning a row of three into a table of one by three. We apply it to a list of three values.

Run the example

We set up a list of three values and call the function.

import numpy as np

a = np.array([1, 2, 3])
b = np.expand_dims(a, axis=0)
print(b.shape)

The result shown is (1, 3). One row and three columns: the same three values now carry a shape with two positions.

A person learning next to a depiction of the function

The same example explained in three steps

academiamastertrader.com.br: more learning material from Academia Master Trader complements this example.

Understand the result

The documentation describes expand_dims as a helper that inserts a new axis into the shape of a list at the position you choose. With position zero the row becomes a table with one row.

The result is easy to check: the values stay in order and only the shape gains a position. Shapes like this prepare data for tables, for example ferry counts between Road Town and Spanish Town.

Place the result

The program keeps the input as it is. It returns a new value or a new list. Each example shows the rule at work on its own values.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read expand_dims as a new position in the shape and confirm the shape by hand.

Academia Master Trader explains numerical tools with examples you can check. More learning material is at academiamastertrader.com.br and via Aprenda.