Numpy Repeat Function Explained In-depth in Python

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

But at 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. Where you want the elements to repeat 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, in the next section, we will be looking at its parameter.

SYNTAX

numpy.repeat(arepeats)

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 need 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:

On completion on 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 be looking 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 at first, we have imported the NumPy module. After this, we have defined our array. After which 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 for the 2-d array. Here we have followed the steps similar to that of the above examples. But here, we can see that although we are considering a 2-d array, our output is 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 similar to the 2 above examples. Here we have used a 2-d array and moreover used the axis parameter. What it does is rather than flattening the array it divides it into subgroups and works on it respectively.

MUST-READ

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 in case you have any unsolved queries feel free to write them below in the comment section. Done reading this why not read about padding in arrays next.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments