VOOZH about

URL: https://www.geeksforgeeks.org/python/numpy-repeat-python/

⇱ numpy.repeat() in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

numpy.repeat() in Python

Last Updated : 28 Mar, 2022

The numpy.repeat() function repeats elements of the array - arr. Syntax : 

numpy.repeat(arr, repetitions, axis = None)

Parameters : 

array : [array_like]Input array. 
repetitions : No. of repetitions of each array elements along the given axis.
axis : Axis along which we want to repeat values. By default, it returns 
 a flat output array.

Return : 

An array with repetitions of array - arr elements as per repetitions, number of times 
we want to repeat arr 

Code 1 : 

Output : 

arr : 
 [0 1 2 3 4]

Repeating arr 2 times : 
 [0 0 1 1 2 2 3 3 4 4]
Shape : (10,)

Repeating arr 3 times : 
 [0 0 0 ..., 4 4 4]
Shape : (15,)

Code 2 : 

Output : 

arr : 
 [[0 1 2]
 [3 4 5]]

Repeating arr : 
 [[0 0 1 1 2 2]
 [3 3 4 4 5 5]]
arr Shape : 
 (12,)

Repeating arr : 
 [[0 1 2]
 [0 1 2]
 [3 4 5]
 [3 4 5]]
arr Shape : 
 (12,)

Repeating arr : 
 [[0 0 0 ..., 2 2 2]
 [3 3 3 ..., 5 5 5]]
arr Shape : 
 (18,)

References : https://numpy.org/doc/stable/reference/generated/numpy.repeat.html Note : These codes won’t run on online IDE's. Please run them on your systems to explore the working .

Comment