Dimension expansion
Expand rows or columns according to a uniform or per-position count.
NumPy · Function
Repeats array elements a specified number of times. Use axis to repeat along a dimension, or omit it to flatten the input first.
np.repeat creates a new array by repeating each input element according to repeats. With axis set, repetition occurs along that dimension; when axis is omitted, the input is flattened first, and a scalar count applies uniformly while an integer array supplies per-element counts.
numpy.repeat(a, repeats, axis=None)| Name | Type | Description |
|---|---|---|
a | array_like | Input array. |
repeats | int or array of ints | Number of repetitions for each element; an integer or integer array. |
axis | int, optional | Axis along which to repeat values. If omitted, the input is flattened. |
An array with repeated elements.
import numpy as np
x = np.array([[1, 2], [3, 4]])
print(np.repeat(x, [1, 2], axis=0))
# [[1 2]
# [3 4]
# [3 4]]Repeats the first row once and the second row twice along axis 0.
Expand rows or columns according to a uniform or per-position count.
Turn grouped observations into repeated samples based on integer frequencies.
Construct repeated label or feature sequences for vectorized processing.
np.repeat repeats array elements and returns the resulting array. The repeats argument can be one integer or an integer array, and axis controls the dimension along which repetition occurs.
Follow-up: What happens when axis is omitted? The input is flattened before repetition, so the result is flat regardless of the original shape.
With axis=1, each element along every row is emitted consecutively according to the repeat count. For example, repeating a two-column array three times along axis 1 produces three adjacent copies of each column value.
Follow-up: How can different columns use different counts? Pass an integer array whose shape broadcasts to the selected axis, such as [1, 2] for two columns.
repeats may be a scalar integer for a uniform count or an integer array for varying counts. When an array is used, NumPy broadcasts it to fit the shape of the chosen axis.
Follow-up: What should be checked when counts vary by position? Ensure the counts array is broadcast-compatible with that axis; otherwise the call is invalid.
numpy.tilenumpy.concatenatenumpy.ndarray.flatten