Construct a regular tiled layout
Create repeated rows or columns from an existing array.
NumPy · Function
Constructs an array by repeating an input along its axes according to reps. NumPy aligns the input rank and repetition specification by adding leading dimensions or leading repetition counts of one.
np.tile constructs a new array by repeating the input along each axis according to reps. The number of dimensions is the larger of reps length and the input rank; missing dimensions are handled by prepending axes or repetition counts of one.
numpy.tile(A, reps)| Name | Type | Description |
|---|---|---|
A | array_like | The input array. |
reps | array_like | The number of repetitions of A along each axis. |
The tiled output array, as an ndarray.
import numpy as np
values = np.array([1, 2, 3])
result = np.tile(values, (2, 1))
print(result)
# [[1 2 3]
# [1 2 3]]Creates a two-row array by repeating the one-dimensional input once along its existing axis.
Create repeated rows or columns from an existing array.
Generate repeated blocks for matrix-like data.
Create explicit repeated data when a materialized array is required.
np.tile repeats an array according to the counts in reps and returns the tiled output array. If reps has d entries, the result rank is max(d, A.ndim).
Follow-up: How is a one-dimensional input treated with two repetition counts? It is promoted by adding a leading axis, so its shape behaves like (1, n).
A is the input array-like object, while reps specifies repetitions along each axis. A scalar repetition is accepted, and a sequence supplies one count per represented axis.
Follow-up: What happens when A has more dimensions than reps? Reps is expanded by prepending ones until it matches A.ndim.
The function returns an ndarray containing the repeated input. For example, tiling [0, 1, 2] with (2, 2) produces two rows, each containing the input twice.
Follow-up: When should broadcasting be used instead? Use broadcasting operations when the goal is compatible-shape computation rather than materializing repeated data.
numpy.repeatnumpy.broadcast_to