How to Use NumPy repeat() in Python?
Python's incredibly strong NumPy module enables us to carry out intricate and computationally intensive calculations. Numerous functions enable us to work with arrays and make use of them as needed. One such function is called the NumPy repeat() function.
The repeat() function in NumPy enables you to repeat an array's elements along with the specified repeat argument. It gives back an array of many elements that have the same form as the input array's specified axis.
Syntax of NumPy repeat()
The syntax for NumPy repeat() is :
Parameters of NumPy repeat()
The various parameters that take in NumPy repeat() are:
- arr:
This mandatory parameter represents the input array that is to be rotated. - repetitions:
This mandatory parameter represents the number of times an input array is to be rotated. - axis:
This optional parameter represents the axis along which we want the elements of an array to be repeated.
Return Type of NumPy repeat()
When we use the NumPy repeat() function, an array is returned which contains the repeated elements of the original array. It returns an output array of the same shape as the input array for one-dimensional arrays, and for multi-dimensional arrays, it returns a flattened array.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Examples of NumPy repeat()
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
How to Repeat a Single Number?
To repeat a single number, we use the NumPy repeat() function, with an integer as input and the number of repetitions that we want.
Output:
How to Repeat a One-dimension List of Numbers Multiple Times?
To repeat a one-dimension array, we will use the NumPy repeat() function, and we can keep an arbitrary number for the number of repetitions.
Output:
Turn Learning into Career Growth
How to Repeat Two-dimension NumPy Array Elements?
Just as we repeated the elements of a one-dimension list, we can repeat the elements of a two-dimension list too. The only difference is that the output will be a flattened array that will contain repeated elements from the two-dimension list.
Output:
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
How to Repeat Elements Down the Columns?
Just as we can repeat the elements of a two-dimensional array, we can also repeat elements down the columns and across the rows. To do this, we have to utilize the optional parameter axis, and set it to 0.
Output:
How to Repeat Elements Across the Rows?
To repeat elements of a two-dimensional array across the rows, we need to use the axis parameter and set it to 1.
Output:
Repeat Array Elements Along with Axis = 1
When we repeat the array elements using the axis as 1, we repeat the elements across rows.
Output:
Repeat Array Elements Along with Axis = 0
When we repeat the array elements using the axis as 0, we repeat the elements down the columns.
Output:
Conclusion
- In this article, we learned about NumPy repeat(), a function that is used to repeat the elements of an array by a factor of k times.
- The output array returned after we use NumPy repeat() is always a one-dimensional flattened array.
- With the help of some examples, we were able to understand the functioning of NumPy repeat() in a much more comprehensive manner.
- To repeat elements down the columns of a two-dimensional array, we need to set the optional axis parameter as 0.
- To repeat elements across a row of a two-dimensional array, we need to set the optional axis parameter as 1.