NumPy tile repeats whole array patterns according to repetition counts for each axis

Repeated patterns can be assembled by copying an entire array rather than repeating each individual entry together. NumPy tile constructs such patterns using repetition counts. Academia Master Trader presents this English lesson for readers in Comoros, using constructed arrays to explain block order, axis counts and the extra dimensions introduced when repetition counts require them.

A vector repeats as a complete pattern

After import numpy as np, define v = np.array([2,5,8]). The call np.tile(v,2) returns [2,5,8,2,5,8]. The complete three-entry pattern appears twice in sequence.

This differs from repeating each entry next to itself. The call np.repeat(v,2) returns [2,2,5,5,8,8]. Both results contain six entries, but their order expresses a different operation. Read the intended pattern before choosing between tile and repeat.

The repetition count is not the desired final length. A count of two means two copies of the input pattern along the relevant axis. For this vector, multiplying its length three by that count gives an output length of six.

NumPy tiled vector

Several repetition counts can add a leading dimension

The call np.tile(v,(2,3)) returns a matrix with shape (2,9). Its two rows are both [2,5,8,2,5,8,2,5,8].

The vector starts with shape (3,). Because the repetition tuple contains two counts, NumPy treats the input as shape (1,3) before applying them. The first count repeats that row twice, and the second repeats its three-entry pattern three times across columns.

This alignment comes from the leading dimension added to the vector. It does not mean that every repetition count applies directly to an axis already present in the original input. Write the promoted shape beside the repetition tuple when explaining the resulting dimensions.

Short repetition tuples align with the final axes

Define m = np.array([[1,2],[3,4]]). The call np.tile(m,2) returns [[1,2,1,2],[3,4,3,4]], with shape (2,4).

The single count is treated as (1,2) for this two-dimensional input. The matrix does not double in both directions. Its row count stays two, while its column count doubles to four.

The call np.tile(m,(2,1)) instead returns [[1,2],[3,4],[1,2],[3,4]], with shape (4,2). The two-row block repeats vertically, while each row retains its original width. Compare the block boundaries as well as the final dimensions.

The order inside each copied block remains the input order. Tile does not sort the pattern or interpolate values between copies. A repeated boundary therefore joins the final entry of one copy directly to the first entry of the next.

Repetition is separate from arithmetic broadcasting

Tile constructs a repeated output array. If the goal is only to combine a smaller array with a larger one through compatible arithmetic, broadcasting may express that calculation without explicitly building all repeated copies.

Choose tile when the repeated arrangement itself is needed. Keep the input shape, repetition tuple and expected output shape together so another reader can reconstruct the pattern without guessing how the counts align.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Describe whether the task repeats whole patterns or individual entries before choosing an array operation.