NumPy stack joins equal shaped arrays along a new axis

Combining arrays can either extend an existing axis or add a new one. Those choices produce different structures even when the values come from the same inputs. Academia Master Trader explains NumPy stack in English for readers in Latvia. Two short one dimensional arrays let us see where the new axis appears and how that position changes the output. We will record shapes before following individual values. The examples describe data organisation, not financial outcomes or additional observations beyond the supplied inputs.

Start with two equal shapes

After import numpy as np, define a = np.array([8,3,6]) and b = np.array([2,9,4]). Each input has shape (3,): one axis with three positions. Array a contains eight, three and six; array b contains two, nine and four. We will supply them in the sequence (a,b), so a is the first input and b is the second.

The NumPy documentation requires every input to stack to have the same shape. This is more specific than having the same total number of elements. For our inputs, the full shape matches directly. Stack then joins them along a new axis, so the output has one more dimension than either input. Starting from one dimensional arrays, we will obtain a two dimensional result.

Put the new axis first

The call np.stack((a,b),axis=0) gives [[8,3,6],[2,9,4]] with shape (2,3). The new first axis has length two because we supplied two arrays. The original three position axis comes after it. In a table description, a becomes the first row and b becomes the second row. The values inside each input keep their original order.

Follow six to make the position clear. It starts at index two in a and appears at (0,2) in the output. Four starts at index two in b and appears at (1,2). The first coordinate identifies which input array supplied the value; the second identifies the original position within that input. This is the role of the new first axis.

Put the new axis last

The call np.stack((a,b),axis=1) gives [[8,2],[3,9],[6,4]] with shape (3,2). Now the original three position axis comes first and the new two position axis comes second. Each row pairs values that had the same index in the inputs. Eight is paired with two, three with nine and six with four.

Six now appears at (2,0) and four at (2,1). The first coordinate records their original position, while the second identifies their input array. Nothing has been sorted or averaged. The selected axis determines where the new input identifying dimension sits in the result. For these one dimensional inputs, axis minus one also selects the last position for the new axis.

NumPy stack

Read each panel from the same inputs

The figure shows both one dimensional inputs and the two output matrices. Each result comes directly from a and b. The panels are not a sequence in which the first output is transformed into the second. Read the call label together with the shape: axis zero gives (2,3), while axis one gives (3,2). Both outputs contain six elements from the supplied inputs.

The axis argument names the position of the new axis in the result. It is not an instruction to select an existing axis from a and change its length. This wording matters because each input has only one axis, yet the new axis may be placed before or after it. The result's axis order is the reference for interpreting the argument.

Compare a new axis with an existing axis

The call np.concatenate((a,b),axis=0) would give [8,3,6,2,9,4] with shape (6,). Concatenate extends the existing axis here. Stack instead introduces a new axis and gives a two dimensional arrangement. The six elements alone cannot tell us which operation was used. The shape and the grouping of values distinguish the results.

If c = np.array([7,5]), its shape (2,) does not match a's shape (3,). Stack does not fill a missing third position or repeat a value automatically. The equal shape requirement must be met before these arrays can be stacked. Check the complete input shapes rather than expecting a convenient printed layout to settle their compatibility.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the matching input shapes, count the input arrays and choose where their new axis will appear. Then trace two values with the same original index. This makes the extra dimension meaningful instead of treating stack as another name for any way of combining arrays.