Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallnp.repeat() copies each element of an array in place along one axis. The axis argument decides whether you get repeated rows, repeated values within each row, or a flattened one-dimensional result. np.tile() works differently: it repeats the whole array as a block. If you want [1, 1, 2, 2], use repeat. If you want [1, 2, 1, 2], use tile.
The signature and the default behavior
In the stable NumPy 2.5 reference, the call is numpy.repeat(a, repeats, axis=None). The input a can be any array-like object. repeats is either a single integer applied to every position, or an array of integers with one count per position along the chosen axis.
The default is the detail that catches most people. With axis=None, NumPy flattens the input before repeating, so a two-dimensional array comes back one-dimensional:
import numpy as np
x = np.array([[1, 2], [3, 4]])
np.repeat(x, 2)
# array([1, 1, 2, 2, 3, 3, 4, 4])
Pass an explicit axis whenever you want the two-dimensional structure preserved.
#1 Best Overall
Repeating rows with axis=0
For an array of shape (rows, columns), axis 0 is the row dimension. Repeating along axis 0 duplicates entire rows, each copy placed directly after the original.
Repeating every row by the same count
np.repeat(x, 2, axis=0)
# array([[1, 2],
# [1, 2],
# [3, 4],
# [3, 4]])
A scalar count multiplies the number of rows by that count, and the number of columns is unchanged.
Repeating individual rows by different counts
np.repeat(x, [1, 2], axis=0)
# array([[1, 2],
# [3, 4],
# [3, 4]])
When you pass a sequence, it must contain one count per row. Row 0 appears once and row 1 appears twice. A sequence whose length does not match the number of rows along that axis raises a ValueError.
Repeating columns with axis=1
Axis 1 is the column dimension. Repeating along axis 1 does not copy whole columns. It repeats each value in place within its row, so the copies sit next to each other and the number of columns grows. This is what people usually mean by “repeating columns,” but the mechanism is element-level.
Recommended Free Tools
Repeating every value across each row
np.repeat(x, 3, axis=1)
# array([[1, 1, 1, 2, 2, 2],
# [3, 3, 3, 4, 4, 4]])
Each value appears three times in a row before the next value begins. The shape changes from (2, 2) to (2, 6).
Repeating individual columns by different counts
np.repeat(x, [1, 3], axis=1)
# array([[1, 2, 2, 2],
# [3, 4, 4, 4]])
Column 0 is kept once and column 1 is repeated three times, so the result has four columns. The same length rule applies: the sequence needs one entry per column.
Predicting the output shape
Once you know which axis you are changing, the output shape is predictable. For an input of shape (m, n):
| Call | Output shape | What changes |
|---|---|---|
np.repeat(a, k) |
(m*n*k,) |
Input is flattened; every element appears k times |
np.repeat(a, k, axis=0) |
(m*k, n) |
Number of rows multiplied by k |
np.repeat(a, k, axis=1) |
(m, n*k) |
Number of columns multiplied by k |
np.repeat(a, counts, axis=0) |
(sum(counts), n) |
Each row repeated by its own count |
np.repeat(a, counts, axis=1) |
(m, sum(counts)) |
Each column repeated by its own count |
The rule for a sequence of counts is that the axis length becomes the sum of those counts. Check .shape after each call; it is the fastest way to catch an axis mistake.
repeat() versus tile()
The two functions differ in what they copy. repeat duplicates each element; tile duplicates the full pattern. The same 1-D input shows the difference:
Rank #4
| Input | np.repeat(a, 2) |
np.tile(a, 2) |
|---|---|---|
[1, 2] |
[1, 1, 2, 2] |
[1, 2, 1, 2] |
On a two-dimensional array, tile with a scalar repeats horizontally, and a tuple controls each dimension separately:
x = np.array([[1, 2], [3, 4]])
np.tile(x, 2)
# array([[1, 2, 1, 2],
# [3, 4, 3, 4]])
np.tile(x, (2, 1))
# array([[1, 2],
# [3, 4],
# [1, 2],
# [3, 4]])
The reps tuple is aligned to the input’s dimensions. If reps has more dimensions than the input, NumPy prepends dimensions to the input. If the input has more dimensions than reps, NumPy prepends ones to reps. For example, np.tile([1, 2], (2, 2)) treats the input as shape (1, 2) and returns [[1, 2, 1, 2], [1, 2, 1, 2]].
The table below compares the two on the axes that matter when choosing:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsBest Value
| Aspect | repeat |
tile |
|---|---|---|
| Unit copied | Each element, in place | The whole array, as a block |
| Count control | One scalar, or one count per position on a single axis | One repetition count per dimension, via reps |
| Behavior without an axis argument | Flattens the input | Keeps the input’s dimensions |
Typical result from [1, 2] with count 2 |
[1, 1, 2, 2] |
[1, 2, 1, 2] |
Which one to use
- Each element needs to be duplicated along one axis: use
np.repeat, and setaxisexplicitly. - The whole array pattern needs to be repeated as a block: use
np.tilewith a scalar or arepstuple. - The repeated data exists only to match shapes in an arithmetic operation: use broadcasting instead of building a copy. The NumPy
tilereference states: “Although tile may be used for broadcasting, it is strongly recommended to use numpy’s broadcasting operations and functions.”
For example, adding a per-column offset to every row needs no copy at all:
x = np.array([[1, 2], [3, 4]])
x + np.array([10, 20])
# array([[11, 22],
# [13, 24]])
Common mistakes
- Omitting
axis. The result is flattened, and a row or column operation that expected a two-dimensional array fails later with a shape error. - Mismatched count sequences. A list whose length differs from the axis length raises a
ValueError. Count the rows or columns before writing the list. - Expecting whole-column copies from
axis=1. Values repeat in place within each row. If you need[1, 2, 1, 2]-style column blocks, that is atileresult. - Choosing a function for speed. The NumPy reference documents what each function returns, not how fast it runs. Pick the function that produces the shape you need.
The behavior described here follows the stable NumPy 2.5 reference. If your installed version differs, confirm the signature with help(np.repeat) or the documentation for that version.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




