NumPy tile repeats a whole pattern, not each value in a group

A repeated pattern and a repeated element are different structures. In NumPy, tile constructs an array by repeating the input array. This makes it useful to read the input as a block: the order inside that block carries through to each repetition. The result can be longer or have more dimensions, depending on the repetition argument and the shape of the input.

Understanding the block is more useful than simply counting how many times a number appears. Two operations may produce outputs with the same number of elements while arranging those elements differently. A clear explanation therefore needs the input, the repetition argument, the resulting order and the resulting shape.

Start with a two-value pattern

Suppose a one-dimensional array contains 2 followed by 5. Asking tile for three repetitions produces 2, 5, 2, 5, 2, 5. Each copy preserves the two-value pattern. The first copy occupies the first two positions, the second occupies the next two, and the third occupies the final two.

np.tile(a, 3)

The repetition argument counts copies of the input pattern in this example. It does not mean three extra copies in addition to the original. Two values repeated as a block three times give six output elements. Keeping the copy count explicit helps explain both the length and the arrangement of the output.

Original diagram showing the input pattern 2,5 repeated as three blocks. The output order is 2,5,2,5,2,5.

The diagram places a border around each copy. Those borders are explanatory labels, not additional data stored by tile. The actual output is one array. A reader should be able to recover its full sequence without relying only on the number of boxes or on colour.

Contrast blocks with individual elements

For the same input 2,5, repeating each element three times gives 2,2,2,5,5,5. That is the behaviour of repeat with a scalar repetition count. Tile instead gives 2,5,2,5,2,5. Both outputs contain six values, including three occurrences of 2 and three occurrences of 5, but their positions differ.

np.repeat(a, 3)

This comparison shows why a frequency count alone cannot describe an array transformation. If later calculations depend on neighbours or position, the arrangement matters. State whether the intended operation repeats the whole pattern or repeats the individual elements before choosing the function.

Neither operation creates a new observation from the outside world. A longer output is a constructed arrangement of the input. If the source contains two recorded values, the six output positions should not be described as six independently recorded values merely because the array is longer.

Read a repetition tuple by dimension

A tuple can describe repetitions along multiple dimensions. With our one-dimensional input 2,5, the argument (2,3) produces two rows. Each row contains the input pattern three times, so each row is 2,5,2,5,2,5. The complete result has shape (2,6).

np.tile(a, (2, 3))

NumPy treats the one-dimensional input as having an added leading dimension for this operation. Its shape is considered as (1,2) before the repetition factors are applied. Repeating that structure twice along the first dimension and three times along the second gives two rows and six columns.

The tuple does not request two occurrences of the value 2 and three occurrences of the value 5. Its entries describe repetitions along dimensions. That interpretation is different from passing per-element repetition counts to repeat, even if the written numbers look similar.

A matrix keeps its internal arrangement

Now take a two-by-two input. Its first row is 2,5 and its second row is 7,9. Using the repetition tuple (2,1) stacks two copies of the entire matrix vertically. The rows are 2,5 ; 7,9 ; 2,5 ; 7,9. The output has four rows and two columns.

np.tile(a, (2, 1))

This does not duplicate the first row beside itself before moving to the second row. The original two-row block stays intact. Describing the block first makes the result easier to read than a vague instruction to double the rows.

For the same matrix, a scalar repetition argument of 2 repeats along the last dimension. The first row becomes 2,5,2,5 and the second becomes 7,9,7,9. The result has two rows and four columns. In this case, NumPy extends the repetition specification with a leading factor of one.

np.tile(a, 2)

Check shape as well as length

The documentation describes dimension promotion explicitly. If the repetition specification has more dimensions than the input, leading dimensions are added to the input. If the input has more dimensions than the repetition specification, leading ones are added to that specification. These rules matter when the expected layout has several dimensions.

A useful reading routine is to write down the input shape, align the repetition factors with its dimensions, and then inspect the output shape and values. Avoid inferring the intended dimensions from the total number of elements alone. Different shapes can hold the same element count while expressing different arrangements.

The documentation also recommends NumPy broadcasting operations and functions when the aim is broadcasting, rather than using tile for that purpose. Learning to recognise a repeated block does not mean that making an explicitly tiled array is the appropriate choice for every numerical calculation.

Academia Master Trader publishes educational material about data organisation and numerical methods. Visit academiamastertrader.com.br to explore further topics and continue learning. When reading tile, identify the whole input pattern first, then explain the repetition factors and the output shape without confusing copied positions with new observations.

Aprenda