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 :
Python
# Python Program illustrating
# numpy.repeat()
import numpy as geek
#Working on 1D
arr = geek.arange(5)
print("arr : \n", arr)
repetitions = 2
a = geek.repeat(arr, repetitions)
print("\nRepeating arr 2 times : \n", a)
print("Shape : ", a.shape)
repetitions = 3
a = geek.repeat(arr, repetitions)
print("\nRepeating arr 3 times : \n", a)
# [0 0 0 ..., 4 4 4] means [0 0 0 1 1 1 2 2 2 3 3 3 4 4 4]
# since it was long output, so it uses [ ... ]
print("Shape : ", a.shape)
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 :
Python
# Python Program illustrating
# numpy.repeat()
import numpy as geek
arr = geek.arange(6).reshape(2, 3)
print("arr : \n", arr)
repetitions = 2
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 1))
print("arr Shape : \n", geek.repeat(arr, repetitions).shape)
repetitions = 2
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 0))
print("arr Shape : \n", geek.repeat(arr, repetitions).shape)
repetitions = 3
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 1))
print("arr Shape : \n", geek.repeat(arr, repetitions).shape)
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://docs.scipy.org/doc/numpy/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 .
Similar Reads
numpy.tile() in Python The numpy.tile() function constructs a new array by repeating array - 'arr', the number of times we want to repeat as per repetitions. The resulted array will have dimensions max(arr.ndim, repetitions) where, repetitions is the length of repetitions. If arr.ndim > repetitions, reps is promoted to
3 min read
Python | Pandas Index.repeat() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.repeat() function repeat elements of an Index. The function returns a new
2 min read
Numpy recarray.repeat() function | Python In numpy, arrays may have a data-types containing fields, analogous to columns in a spreadsheet. An example is [(a, int), (b, float)], where each entry in the array is a pair of (int, float). Normally, these attributes are accessed using dictionary lookups such as arr['a'] and arr['b']. Record array
4 min read
Python - itertools.repeat() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re
2 min read
numpy.multiply() in Python The numpy.multiply() is a numpy function in Python which is used to find element-wise multiplication of two arrays or scalar (single value). It returns the product of two input array element by element.Syntax:numpy.multiply(arr1, arr2, out=None, where=True, casting='same_kind', order='K', dtype=None
3 min read