NumPy stack joins equally shaped arrays along a new axis rather than extending an old one
Two arrays can contain matching sets of positions without belonging in one long sequence. Sometimes the useful result keeps each input together and adds a dimension that identifies which input supplied a value. NumPy stack performs that kind of join. It combines arrays along a new axis instead of extending an axis that already exists.
Academia Master Trader presents this explanation in English for readers in Ghana. The examples concern the structure of numerical arrays only. They do not represent market prices, trading signals or financial results. The aim is to make the relationship between input shapes, the new axis and the final arrangement easy to follow.
Two vectors become a matrix
Let a be the vector [1,2,3] and b the vector [4,5,6]. Each input has shape (3,). The expression np.stack((a,b),axis=0) produces a matrix whose rows are [1,2,3] and [4,5,6]. Its shape is (2,3).
The new first axis has length two because there are two input arrays. The original axis of length three remains present as the second axis. Each input was one-dimensional, and the result is two-dimensional. There are six elements in total, but the extra dimension is the main structural change.

The values are neither added nor averaged. The first row comes from a and the second from b because the inputs were supplied in that order. Reversing the sequence to (b,a) changes their order along the new axis. Stack does not select an order based on the values themselves.
Put the new axis last
With the same vectors, np.stack((a,b),axis=-1) produces the rows [1,4], [2,5] and [3,6]. The output shape is (3,2). The new axis now comes last, so each row brings together values from the same original position in the two inputs.
This arrangement still contains six elements. However, shape (3,2) is different from shape (2,3). One result has three rows and two columns; the other has two rows and three columns. Counting elements alone cannot tell a reader how the axes have been arranged.
For this two-dimensional result, axis one and axis minus one identify the same final axis. A negative axis index counts from the end of the result. It is helpful to describe the intended dimensions in words before choosing the numeric index, especially when the inputs have more than one dimension.
Every input must have the same shape
Stack requires the input arrays to have the same shape. A vector with shape (3,) does not match a matrix with shape (1,3), even though both contain three values. Their numbers of dimensions differ. The visible values alone do not establish compatibility.
Likewise, two vectors of lengths three and four do not meet the requirement. Stack does not invent a fourth value for the shorter vector. Any decision to pad, remove or otherwise reorganise values belongs to an earlier preparation step and needs a reason that comes from the data's intended use.
Shape equality is a structural condition, not proof of matching meaning. Two arrays may have the same shape while containing different units or referring to different periods. The function has no knowledge of those distinctions. Anyone preparing the inputs must decide whether corresponding positions should be grouped together.
Stacking matrices adds a third dimension
Suppose two inputs both have shape (2,3). Stacking them with axis zero produces shape (2,2,3). The first two in that shape records the number of inputs. The next two and the final three come from the two rows and three columns within each input.
Putting the new axis last instead produces shape (2,3,2). The new dimension still has length two, but its position in the shape changes. This is why axis is an instruction about where to insert a dimension, not merely a choice between placing items visually above or beside one another.
A useful check is to mark the new dimension separately when writing the expected shape. Then read the remaining dimensions in their original order. The number of dimensions should increase by one compared with each input, while the new dimension's length should equal the number of arrays supplied.
Contrast stack with concatenate
Concatenate joins along an existing axis. For the vectors [1,2,3] and [4,5,6], concatenation along axis zero produces [1,2,3,4,5,6], with shape (6,). Stack with axis zero instead produces shape (2,3). The two functions preserve the same example values but organise them differently.
The choice therefore starts with the intended structure. If the inputs should become sections of one existing dimension, concatenation may express that intention. If an additional dimension should identify the separate inputs, stack expresses a different relationship. Neither function decides what the added organisation means in the subject being studied.
Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda provides access to the educational material. Write down each input shape, identify where the new axis belongs and compare the output with that description. This keeps values, positions and dimensions separate throughout the join.