NumPy repeat repeats elements along an axis rather than creating new observations

Seeing the same number twice in an output does not tell us why it appears twice. It may come from two measurements, or from a deliberate repetition of one stored value. Academia Master Trader explains NumPy repeat in English for readers in Denmark. A small matrix will show how the repetition count and the axis work together. We will compare a flat result, repeated values within rows and repeated complete rows. These examples describe array structure, not financial outcomes.

Establish the input and the repetition count

After import numpy as np, define a = np.array([[8,3],[2,9]]). The shape is (2,2), meaning two rows and two columns. The first row contains eight and three, and the second contains two and nine. There are four input elements. We will start with a repetition count of two and observe how the output changes when an axis is supplied.

In np.repeat(a,2), two is the total number of appearances requested for each input element in the result. It does not mean keeping the original and then adding two more appearances. Eight appears twice, not three times. Keeping that distinction clear makes the output size easier to understand before reading the printed values.

Read the flat default result

The call np.repeat(a,2) gives [8,8,3,3,2,2,9,9] with shape (8,). When axis is omitted, repeat uses the flattened input and returns a flat output. The input sequence is read as eight, three, two and nine. Each element is repeated immediately after itself before the operation moves to the next input element.

This is not the same arrangement as writing the whole sequence twice. Eight, three, two, nine, eight, three, two, nine would repeat the block. Our output instead groups each element's requested appearances together. Follow the first three output positions: eight, eight and three. That small check already distinguishes element repetition from repeating the complete input block.

Repeat along the column axis

The call np.repeat(a,2,axis=1) gives [[8,8,3,3],[2,2,9,9]] with shape (2,4). The two rows remain separate. Within each row, each column value appears twice in succession. The first row becomes eight, eight, three and three. The second becomes two, two, nine and nine. The output still has two rows but now has four columns.

Axis one identifies the dimension along which values are repeated. The repetition count controls how many appearances each element has there. The other dimension keeps its original length. In this example, four input elements produce eight output elements, but the two dimensional arrangement is retained because we explicitly selected the column axis rather than the flat default.

NumPy repeat

Repeat along the row axis

The call np.repeat(a,2,axis=0) gives [[8,3],[8,3],[2,9],[2,9]] with shape (4,2). The original first row appears twice before the original second row appears twice. The order inside each row remains unchanged. There are now four rows and two columns. This differs from placing a second complete copy of the matrix after the first.

The figure compares these two explicit axes with the flat default. Each result begins from the same input, not from the preceding result. Read its label and shape together. The column result has shape (2,4), the row result has (4,2) and the flat result has (8,). All contain eight elements, so the element count alone cannot identify the arrangement.

Different positions can have different counts

The documentation also allows an array of repetition counts. For np.repeat(a,[1,2],axis=0), the result is [[8,3],[2,9],[2,9]] with shape (3,2). The first row appears once and the second appears twice. Here the two counts correspond to the two positions along the selected row axis. They are not a request for a final shape of one by two.

Repetition changes the number of output elements, but it does not create independent measurements of the world. If a value represents one recorded observation, repeating it in an array is still a data operation on that recorded value. Explain repeated data as repeated data. A larger output array is not by itself evidence that more observations were collected.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Write the input shape, repetition count and selected axis before reading the result. Then count the appearances of one element and confirm the output shape. This keeps the meaning of repeat clear even when several output arrangements contain the same total number of elements.