w3resource

NumPy: numpy.repeat() function

numpy.repeat() function

The numpy.repeat() function is used to repeat elements of an array. It takes an array and a repetition count as inputs and outputs a new array with the original array elements repeated based on the repetition count.

The function is useful in data processing, scientific computing and image processing, where the repeated elements can be used in feature extraction, data augmentation.

Syntax:

numpy.repeat(a, repeats, axis=None)
NumPy manipulation: numpy.repeat() function

Parameters:

Name Description Required /
Optional
a Input array. Required
repeats The number of repetitions for each element. repeats is broadcasted to fit the shape of the given axis. Required
axis The axis along which to repeat values. By default, use the flattened input array, and return a flat output array. Optional

Return value:

repeated_array [ndarray] Output array which has the same shape as a, except along the given axis.

Example: Repeating a scalar using numpy.repeat()

>>> import numpy as np
>>> np.repeat(5,3)
array([5, 5, 5])

In the above code, the numpy.repeat() function is used to repeat the scalar value 5, three times. The first argument of the function is the value to be repeated and the second argument is the number of times to repeat the value.

Pictorial Presentation:

NumPy manipulation: repeat() function

Example: Repeating elements of an array using numpy.repeat()

>>> import numpy as np
>>> a = np.array([[3,5], [7,9]])
>>> np.repeat(a, 4)
array([3, 3, 3, 3, 5, 5, 5, 5, 7, 7, 7, 7, 9, 9, 9, 9])
>>> np.repeat(a, 4, axis=1)
array([[3, 3, 3, 3, 5, 5, 5, 5],
       [7, 7, 7, 7, 9, 9, 9, 9]])

In the first example, a 2-dimensional array 'a' is created using numpy.array() function. The np.repeat() function is then applied on the array a with repeat count 4, which results in each element of a being repeated 4 times, producing a 1-dimensional array of length 16.
In the second example, the same array 'a' is used, but this time the np.repeat() function is applied with repeat count 4 along axis=1. This results in each element of a being repeated 4 times along the columns (i.e. horizontally) and the resulting array is 2-dimensional with the same number of rows and 4 times the number of columns.

Pictorial Presentation:

NumPy manipulation: repeat() function

Example: Repeating Rows in Numpy Arrays

>>> import numpy as np
>>> a = np.array([[3,5], [7,9]])
>>> np.repeat(a, [1,3], axis=0)
array([[3, 5],
       [7, 9],
       [7, 9],
       [7, 9]])

In the above code, a 2x2 NumPy array a is defined with values [[3,5], [7,9]]. The np.repeat() function is then used to repeat the rows of a, with the argument [1,3] indicating that the first row should be repeated once, and the second row should be repeated three times.

Pictorial Presentation:

NumPy manipulation: repeat() function

Python - NumPy Code Editor:

Previous: Tiling arrays tile()
Next: Adding and removing elements delete()



Follow us on Facebook and Twitter for latest update.