What is the numpy.repeat() function in Python?
Overview
The repeat() function in Python is simply used to repeat the elements of an input array.
Syntax
The repeat() function takes the syntax below:
numpy.repeat(a, repeats, axis=None)
Syntax for the repeat() function in Python
Parameter value
The repeat() function takes the following parameter values:
a: This is the input array. It is a required parameter.
repeats: This is the number of times for which the elements of the input array are to be repeated. It is a required parameter.axis: This is the axis along which the repetition is made. It is an optional parameter.
Return value
The repeat() function returns an array of the same shape as the input array.
Example
import numpy as np# creating an input arraya = np.array([1,2,3,4,5])# calling the repeat() functionmy_array = np.repeat(a, 2)print(my_array)
Explanation
- Line 1: We import the
numpymodule. - Line 3: We create an input array,
a, using thearray()function. - Line 6: We call the
repeat()function and passaand2as arguments. The function will create an array in which the elementawill be repeated twice. - Line 9: We print the new array,
my_array.