Skip to main content

np.repeat()

np.repeat()

Repeat each element of an array after themselves.

Input:
arr : array_like
Input array.
repeats : int or array of ints
The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis.
axis : int, optional (Default flattens array)
The axis along which to repeat values. By default, use the flattened input array, and return a flat output array.
Returns:
An array with repetitions of the original array.
Return Type:
numpy ndarray

np.repeat(3, 4) #Iterate over one element

array([3, 3, 3, 3])

np.repeat([1, 2, 3, 4], 2) # Iterate over an array 

array([1, 1, 2, 2, 3, 3, 4, 4])

arr = np.array([[1, 2], [3, 4]])

# Default behavior (flattened input)
np.repeat(arr, 2)

np.repeat(arr, 2, axis=0)

array([1, 1, 2, 2, 3, 3, 4, 4])

array( [[1, 2], [1, 2], [3, 4], [3, 4]] )


Problems or suggestions about this page? Fill out our feedback form.