## Introduction to NumPy repeat

In Python, NumPy repeat() is a function that is defined as a function to repeat the given elements of the array as many numbers of times specified in the repeat() function and we can define it to work along with axis also. NumPy repeat() function is used to display the element of a given array any number of times and also along the axis which means row-wise such as axis =1 if we want the elements of the first row to be repeated and display the repeated number array same as the array at the input with the same shape and along the same given axis.

### Working of NumPy repeat() Function in Python

In Python, the NumPy module is used to deal with arrays and this module provides a function that enables to display of an array of the repeated element of the array, and this is done by using repeat() function. The repeat() function is used to display or return an array of the repeated element as many numbers of times specified by the user in a function argument.

This function can also be used to return or display the array of the repeated element along with specified axis such axis value as 0 and 1 in a one-dimensional array and single-dimensional array we will have axis value as 0 only. So, we will see axes as an optional parameter but also important when we consider manipulating NumPy arrays using repeat() function.

**Syntax:**

`numpy.repeat(in_arr, no_repeat, axis)`

In the above syntax we can see there are 3 different parameters and they are as follows:

**in_arr:**This parameter is used to specify an input array to get the element that needs to be repeated.**no_repeat:**This parameter is used to specify the number of times the elements to be repeated. This argument will always have value as an integer.**axis:**This parameter is optional which is used to specify the axis such 1D array has 1 axis and the 2D array has 2 axes which mean directions in array dimensions. This parameter by default takes a flat as a value that returns a flat output array if it is one-dimensional array to flattened input array.

This repeat() function of NumPy returns an array containing the repeated elements the number of times specified in the above function. We can also see that it will return an array with the same shape and with the given axis as the given input array.

Now let us see how to use repeat() function in Python program in the below example.

4.5 (6,919 ratings)

View Course

**Example:**

**Code:**

`import numpy as nr`

print("Program to demonstrate repeat() function")

print("\n")

print("The simple elements repetition is as follows:")

sim_res = nr.repeat(9, 4)

print(sim_res)

print("\n")

in_arr = nr.array([[1, 2], [3, 4], [5, 6]])

print("The given input array is as follows:")

print(in_arr)

print("\n")

res = nr.repeat(in_arr, 2)

print("The array with repeated elements with default axis value")

print(res)

print("\n")

res1 = nr.repeat(in_arr, 3, axis=1)

print("The array with repeated elements of each row with axis as 1 is as follows:")

print(res1)

res2 = nr.repeat(in_arr, 2, axis=0)

print("The array with repeated elements of each row with 0 axis is as follows:")

print(res2)

**Output:**

In the above program, we can see we have imported NumPy module first then we have declared an array using an array() function of the NumPy module then we are using repeat() function to repeat the number of elements of the given item which is “9” is printed 4 times, then we have declared an array with 3 items and each printing 2 times wherein first result “res” we have axis value as the default value is flat where we can see all the elements are flattened at one single row. But when we are declaring another result “res1” where we have declared axis =1 in repeat() function then we get elements repetition is done horizontally in row 3 times in a single row. When we have declared axis as 0 then each element repetition is done vertically (downwards) are printed 2 times in separate rows and also for the rest of the elements in the given input array. These output results can be seen in the above screenshot of the output of the above-given program.

In this article, we have to note that when we are using a one-dimensional array which may consider only one element and it is easy to know that when we apply repeat() function to one-dimensional then it will flatten the input array but that would not make much easy to understand why it will flatten. Now if we have two-dimensional array if we don’t specify the axis then it will by default it will flatten 2D array into 1D array by placing all the elements with its repetition in a single dimensional array as one single row. This repeat() function is used when we want to display duplicate of all the rows of a multidimensional array by keeping axis value as 0, but this duplication of rows is not possible in one-dimensional as it will throw an error so to overcome this we can expand the array from 1D to 2D using another NumPy function expand_dims()and then we can use axis value as 0 to get the row elements duplication. So there is also another NumPy function tile() which is also used for repetition, but not for single element as repeat() but the entire array.

### Conclusion

In this article, we conclude that when we want to display an array with repeated elements then in Python we repeat() function in the NumPy module. In this article, we also saw what is the syntax and its parameters declared to display accordingly along with specifying the input array, a number of times the elements to be repeated, and also an optional axis specification. In this article, we also saw an example of using the repeat() function with an optional parameter and also with the axis parameter with different values.

### Recommended Articles

This is a guide to NumPy repeat. Here we discuss the introduction and working of NumPy repeat() function in python respectively. You may also have a look at the following articles to learn more –