numpy.repeat() in Python
numpy.repeat() in Python
The numpy.repeat() function repeats the elements of an array.
Syntax
numpy.repeat(a, repeats, axis=None)
Parameter
The numpy.repeat() function consists of three parameters, which are as follows:
a: This parameter represents the input array.
repeats: This parameter represents the number of repetitions for each element.
axis: It signifies the axis along which to repeat the values. By default, it uses the flattened input array, and returns a flat output array.
Return
This function returns the output array (ndarray) which has the same shape as parameter ‘a’, except along the given axis.
Example 1
# Python Program explaining # numpy.repeat() function import numpy as np #Working on 1D arr = np.arange(5) print("arr value: ", arr) rep = 2 a = np.repeat(arr, rep) print("\nRepeating arr 2 times : \n", a) print("Shape : ", a.shape) rep = 3 a = np.repeat(arr, rep) print("\nRepeating arr 3 times : \n", a) print("Shape : ", a.shape)
Output
arr value: [0 1 2 3 4] Repeating arr 2 times : [0 0 1 1 2 2 3 3 4 4] Shape : (10,) Repeating arr 3 times : [0 0 0 1 1 1 2 2 2 3 3 3 4 4 4] Shape : (15,)