NumPy repeat vs tile: Key Differences Explained
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
NumPy’s repeat and tile both create larger arrays from an input array, but they differ in how they duplicate elements: repeat duplicates each element a specified number of times consecutively, while tile repeats the entire array as a block. This article explains exactly how each function works, how to use them for rows and columns, and when to choose one over the other.
Formula
For repeat: the output has length len(arr) * reps if axis is None (flattened), or the size of the specified axis multiplied by reps. Each element at index i in the input appears reps times consecutively in the output.
For tile: the output has length len(arr) * reps when reps is an integer, but the entire array is repeated as a block. If reps is a tuple, the array is tiled along each axis according to the corresponding value.
Worked Example
Using the default input arr = [1,2,3] and reps = 2:
np.repeat([1,2,3], 2)→[1,1,2,2,3,3]np.tile([1,2,3], 2)→[1,2,3,1,2,3]
For a 2D array, say arr = [[1,2],[3,4]]:
np.repeat(arr, 2, axis=0)→ repeats each row:[[1,2],[1,2],[3,4],[3,4]]np.repeat(arr, 2, axis=1)→ repeats each column:[[1,1,2,2],[3,3,4,4]]np.tile(arr, (2,1))→ stacks the whole array vertically:[[1,2],[3,4],[1,2],[3,4]]np.tile(arr, (1,2))→ places two copies side by side:[[1,2,1,2],[3,4,3,4]]
How repeat Works
The numpy.repeat function takes three parameters: a (input array), repeats (number or list of numbers), and axis (optional). When axis is not specified, the array is flattened first, and each element is repeated repeats times. If repeats is a list, its length must equal the size of the array (or the axis dimension), and each element gets its own repetition count.
For example, np.repeat([1,2,3], [1,2,3]) yields [1,2,2,3,3,3]. This is useful when you want uneven repetition.
How tile Works
The numpy.tile function takes two parameters: A (input array) and reps (number or tuple). If reps is an integer, the array is repeated that many times along the last axis (columns for 2D). If reps is a tuple, its length must be at least the number of dimensions of A. The tuple values specify how many times to repeat along each axis, starting from the last axis.
For instance, np.tile([[1,2],[3,4]], (2,3)) creates a 4×6 array where the original 2×2 block appears in a 2×3 grid.
Repeating Rows vs Columns
To repeat rows with repeat, set axis=0. To repeat columns, set axis=1. With tile, use a tuple like (n,1) to repeat rows (vertical stacking) and (1,n) to repeat columns (horizontal stacking).
For example, given arr = [[1,2],[3,4]]:
- Repeat rows twice:
np.repeat(arr, 2, axis=0)ornp.tile(arr, (2,1)) - Repeat columns twice:
np.repeat(arr, 2, axis=1)ornp.tile(arr, (1,2))
When to Use Which
Choose repeat when you need each element duplicated individually, such as when expanding a dataset by repeating each measurement. Choose tile when you need to replicate the entire array as a block, such as creating a checkerboard pattern or tiling an image.
If you need uneven repetitions, repeat is the only option. If you need to repeat along multiple axes simultaneously, tile is more convenient.
Common Mistakes and Performance
A frequent mistake is confusing the two when repeating rows/columns. Remember: repeat duplicates elements, tile duplicates blocks. Another mistake is forgetting that repeat without axis flattens the input, which may not be intended.
Performance-wise, both are efficient for moderate sizes. For very large arrays, tile may be slightly faster because it copies whole blocks, but the difference is usually negligible. Memory usage is proportional to the output size, so be cautious with large inputs.
Frequently Asked Questions
Can I repeat rows and columns at the same time with repeat? Yes, but you can only specify one axis at a time. To repeat both, you need two calls: first repeat along axis=0, then repeat along axis=1. Alternatively, use tile with a tuple like (2,3).
What does np.repeat do when axis is None? It flattens the input array into 1D and then repeats each element. This is often unexpected if you have a 2D array and forget to specify axis.
Is np.tile the same as np.broadcast_to? No. broadcast_to creates a read-only view with zero memory copy, while tile creates a new array with actual data. Use broadcast_to for memory efficiency when you only need to read.
How do I repeat each row a different number of times? Use np.repeat with a list of repetition counts. For example, np.repeat(arr, [1,2], axis=0) repeats the first row once and the second row twice.
Does tile work with 3D arrays? Yes, tile works for any dimension. The reps tuple length should match the number of dimensions, but if it’s shorter, the array is treated as having extra leading dimensions of size 1.
Related
Formula used
in arr | Input array (comma-separated) | - | 1,2,3 | 0 in reps | Number of repetitions | - | 2 | 0 sel axis | Axis for repeat | None=0, axis=0=1, axis=1=2 | 0 out rep_out | repeat result | - | repeat(arr, reps, axis) | 0 out tile_out | tile result | - | tile(arr, reps) | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.