NumPy stack joins equal shaped arrays by introducing a new dimension
Combining arrays can preserve each input as a separate slice instead of extending an existing dimension. NumPy stack does this by introducing a new axis. Academia Master Trader explains the operation in English for readers in Vatican City. Small constructed vectors and matrix shapes make the distinction visible. The examples focus on how input order and the position of the new axis determine the output arrangement.
Two vectors can become two rows
After import numpy as np, define x = np.array([2,4,6]) and y = np.array([3,5,7]). Both inputs have shape (3,). The call np.stack((x,y)) returns [[2,4,6],[3,5,7]]. The output has shape (2,3): two input arrays are arranged along a newly created first axis.
The first output row contains x and the second contains y. Within each row, the original entry order is preserved. The default axis is zero, so np.stack((x,y),axis=0) gives the same result. The new dimension identifies which input array supplied the corresponding slice, according to the order in the supplied sequence.

Put the new dimension at the end
The call np.stack((x,y),axis=1) returns [[2,3],[4,5],[6,7]]. The output shape is (3,2). Each row pairs entries at the same position in the two original vectors. Two is paired with three, four with five and six with seven.
For these one dimensional inputs, axis minus one selects that same final position for the new dimension. Thus np.stack((x,y),axis=-1) gives the same result as axis one. This equality depends on the dimensions of the result. State where the new axis appears instead of assuming that the same axis number has a universal row or column meaning.
Equal shape is a requirement
Every input array in the sequence must have the same shape. A vector with three entries and a vector with two entries do not meet that requirement. Stack does not automatically fill the shorter vector or repeat its entries to make it fit. Decide the intended representation before combining unequal inputs.
Matching total entry counts is also not enough if shapes differ. A matrix with shape (2,3) and one with shape (3,2) both contain six entries, but they are not equal shaped inputs. Write the complete shapes down before stacking, including every dimension length rather than just the total size.
Matrices gain a third dimension
Suppose p and q are two matrices, each with shape (2,3). The expression np.stack((p,q),axis=0) has shape (2,2,3). The new first dimension has length two because there are two input matrices. Each slice selected along that dimension has the original two by three shape.
With axis one, the result has shape (2,2,3) as well, but the new dimension occupies a different position. Equal printed shapes do not guarantee equal arrangements when two dimension lengths happen to match. With axis two, the result has shape (2,3,2), placing the input selection dimension last. Trace a specific entry when dimension lengths alone cannot distinguish the layouts.
A new axis differs from a longer existing axis
For the vector example, np.concatenate((x,y)) returns [2,4,6,3,5,7], a vector with six entries. Stack instead returns a two dimensional result. Both operations preserve the supplied numbers and input order, but they encode the grouping differently in the output shape.
Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record each input shape, the input sequence and the position of the new axis. Then check that the output has one more dimension than an individual input array. This makes array grouping explicit without turning a new layout into new information.