NumPy tile repeats an array as blocks according to repetition counts by axis

Some array layouts require a whole pattern to appear several times. NumPy tile constructs such a layout by repeating an array as blocks. Academia Master Trader presents this English lesson for readers in Cambodia. Constructed examples explain how repetition counts relate to axes, why a vector can become a matrix, and how block repetition differs from making consecutive copies of each individual entry.

Repeat the complete vector pattern

After import numpy as np, define a = np.array([2,5,8]). The call np.tile(a,2) returns [2,5,8,2,5,8]. The original three entry pattern appears twice in sequence, producing six output entries. The order inside each block stays two, five, eight.

This differs from [2,2,5,5,8,8], where each entry appears twice consecutively. Both arrangements contain two copies of each value, but their positions differ. A later calculation that depends on neighboring values can therefore receive a different arrangement. Choose the intended layout before deciding whether to repeat blocks or individual entries.

NumPy tile

Two repetition counts can create a matrix

The call np.tile(a,(2,1)) returns [[2,5,8],[2,5,8]]. The output has shape (2,3). NumPy treats the vector as shape (1,3) for this two dimensional replication, then repeats that row twice. The second repetition count, one, keeps the three entry pattern once across each row.

The call np.tile(a,(2,2)) returns [[2,5,8,2,5,8],[2,5,8,2,5,8]]. Its shape is (2,6): two rows, each containing two blocks of the vector. The repetition counts are not the requested final shape. They describe how many times the input layout is replicated along each corresponding axis.

A matrix keeps its internal block layout

Define m = np.array([[1,4],[7,9]]). The call np.tile(m,(2,1)) returns [[1,4],[7,9],[1,4],[7,9]]. It stacks two copies of the whole matrix block in the first axis direction. The result has shape (4,2).

Notice the row order: first one and four, then seven and nine, followed by the same two rows again. Tile has not duplicated the first row consecutively before duplicating the second row. The original two row block is the repeated pattern, so its internal row order remains visible in every copy.

A scalar count applies to the last matrix axis

The call np.tile(m,2) returns [[1,4,1,4],[7,9,7,9]]. For a two dimensional input, this single repetition count is treated as (1,2). The first axis is repeated once and the last axis twice, producing shape (2,4).

It does not create a flat vector and does not repeat the matrix twice along every axis. If the desired repetition is in the row direction, write (2,1) explicitly. Supplying one count can be concise, but naming both counts makes the intended matrix layout easier to inspect and explain.

Shape promotion is part of the rule

When the repetition count sequence has more dimensions than the input, NumPy adds leading axes to the input shape. When the input has more dimensions than the count sequence, leading ones are added to the counts. These rules align the shapes before constructing the repeated blocks.

The documentation recommends broadcasting operations rather than tile when broadcasting is the actual purpose. Repeated storage is not automatically necessary merely because an arithmetic operation combines different compatible shapes. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input shape, repetition counts and output shape so each copied block has a clear place in the result.