# Numpy Repeat Function Explained In-depth in Python

Hello geeks, and welcome! In this article, we will cover the NumPy repeat(). Along with that, we will also look at its syntax and parameters for a better overall understanding. Then, we will see the application of all the theory parts through a couple of examples.

But first, let us try to get a brief overview of our function NumPy repeat() through its definition. Suppose you define an array; you define an array. If you want the elements to be repeated multiple times, you can use this function and get your job done easily instead of writing them by hand. It will become more clear as we discuss a few examples. But before that, we will look at its parameters in the next section.

Contents

## Syntax

``numpy.repeat(a, repeats)``

This is the general syntax associated with our function. In the next section, we will cover the various parameters associated with it.

## Parameter

1. a:array_like

This parameter represents the array on which the operation needs to be performed.

2. repeat: int or array of int

This parameter represents how many times does an element of the array needs to be repeated.

3. axis: int

It is an optional parameter that represents the axis along which the values need to be repeated.

## Return

repreated_array:

Upon completion of the code, it returns the repeated array. Which has the same shape as the input array.

## Examples

As we are done with all the theory portion related to NumPy repeat(). In this section, we will look at how this function works and how it helps us achieve our desired output. We will start with an elementary-level example and gradually move our way to more complicated examples.

### 1. Basic example for NumPy repeat()

```#input
import numpy as ppool
a=[1,23,4,5]
print(ppool.repeat(a,2))
```
```#output
[ 1  1 23 23  4  4  5  5]
```

In the above example, we first imported the NumPy module. After this, we have defined our array. After that, we used our syntax along with a print statement. Here, as instructed in the program, we want the elements to be repeated twice. So, our output justifies it. The main motive of this example is to make you aware of the functioning of the syntax. Next, we will see a bit more advanced example.

### 2. Example with Numpy repeat as an array

```import numpy as ppool
a=[1,23,4,5]
print(ppool.repeat(a,[2,1,3,1]))
```
```#output
[ 1  1 23  4  4  4  5]
```

Above, we can see another example. We have followed all the steps similar to that of 1st example. But instead of just using an integer for the purpose of repetition, we have used an array. Here, we have specified the number of times we want each element to return. The output here justifies our input. By doing this, we get a bit more freedom and control.

### 3. Example for a 2-D array

```import numpy as ppool
a=[[1,2,3,4],
[5,6,7,8],
[12,32,45,5]]
print(ppool.repeat(a,[1,2,3,1,
1,4,1,2,
1,2,3,1]))
```
```[ 1  2  2  3  3  3  4  5  6  6  6  6  7  8  8 12 32 32 45 45 45  5]
```

Above, we have considered an example of the 2-D array. Here, we have followed steps similar to those in the above examples. But here, we can see that although we are considering a 2-D array, our output is a flattened 1-D array. In the next example, we will see how to retain the shape of a 2-D array when using the repeat function.

### 4. Example with the axis parameter

```import numpy as ppool
a=[[1,23,4,5],
[2,45,67,52]]

print(ppool.repeat(a,[2,1,3,1],axis=1))
```
```[[ 1  1 23  4  4  4  5]
[ 2  2 45 67 67 67 52]
```

Another example is similar to the 2 above examples. Here, we have used a 2-d array and the axis parameter. What it does is rather than flattening the array, it divides it into subgroups and works on it respectively.

## Conclusion

In this article, we covered the NumPy repeat(). Besides that, we have also looked at its syntax and parameters. For better understanding, we looked at a couple of examples. We varied the syntax and looked at the output for each case. In the end, we can conclude that NumPy repeat() repeats the element of the array.

I hope this article was able to clear all doubts. But if you have any unsolved queries, feel free to write them down below in the comment section.