NumPy Repeat

The numpy.repeat() method takes the repetitions (int) as an input argument, repeats the elements of an array, and returns an array as output. The repetitions can happen row and column-wise if we pass the axis argument else the array will be flattened and then repeated.

NumPy Repeat Syntax

The syntax of numpy.repeat() function is:

numpy.repeat(arepeatsaxis=None)

NumPy Repeat Parameters

The numpy.repeat() method can take 3 parameters:

  • arr – An array-like object as an input argument
  • repeats – The number of repetitions for each element. 
  • axis(optional) – The axis along which to repeat the values. If None, it uses the flattened input array and returns a flattened output array.

NumPy Repeat Return Value

The numpy.repeat() method returns an array with the same shape as arr, except along the given axis.

Example 1: Repeating the Single Dimensional NumPy Array

In the below example, we are using the numpy.repeat() method on the 1-Dimensional NumPy Array. The repetition occurs elements-wise, and each element gets repeated in the 1-D Array as shown.

from array import array
import numpy as np

# NumPy Array
arr = np.array([1, 2, 3])

# Prints original Array
print('Original Array\n', arr)

# repeat the 1-D array 2 times
new_arr = np.repeat(arr,2)

# print the NumPy Array
print('Repeating 1-D Array\n',new_arr)

Output

Original Array
 [1 2 3]

Repeating 1-D Array
 [1 1 2 2 3 3]

Example 2: Repeating the Two-Dimensional NumPy Array

In the previous example, we saw the repetition happens element-wise instead of Array wise.

In the case of a 2-Dimensional Array, the numpy.repeat() method will return a flattened array by default and then repeat the elements.

It means the arrays are first reshaped to single-dimensional before repeating. This happens because we are not passing the axis argument; by default, it takes as none and reshapes into 1-Dimensional Array.

from array import array
import numpy as np

# NumPy Array
arr = np.array([[1, 2, 3],[4,5,6]])

# Prints original Array
print('Original Array\n', arr)

# repeat the 2-D array 2 times
new_arr = np.repeat(arr,2)

# print the NumPy Array
print('Repeating 2-D Array\n',new_arr)

Output

Original Array
 [[1 2 3]
 [4 5 6]]

Repeating 2-D Array
 [1 1 2 2 3 3 4 4 5 5 6 6]

Example 3: Repeating the NumPy Array Row Wise

In most cases, we do not want to flatten the multi-dimensional array; rather, we would need to repeat the array along the axis, i.e., either row-wise or column-wise.

In NumPy Array, the 0th axis represents the row; hence if we need to repeat the array row-wise, we need to pass 0 into the axis argument.

from array import array
import numpy as np

# NumPy Array
arr = np.array([[1, 2, 3],[4,5,6]])

# Prints original Array
print('Original Array\n', arr)

# repeat the 2-D array 2 times along row
new_arr = np.repeat(arr,2,axis=0)

# print the NumPy Array
print('Repeating 2-D Array Row Wise\n',new_arr)

Output

Original Array
 [[1 2 3]
 [4 5 6]]

Repeating 2-D Array Row Wise
 [[1 2 3]
 [1 2 3]
 [4 5 6]
 [4 5 6]]

Example 4: Repeating the NumPy Array Column Wise

In NumPy Array, the 1st axis represents the column; hence if we need to repeat the array column-wise, we need to pass 1 into the axis argument.

from array import array
import numpy as np

# NumPy Array
arr = np.array([[1, 2, 3],[4,5,6]])

# Prints original Array
print('Original Array\n', arr)

# repeat the 2-D array 2 times along Column
new_arr = np.repeat(arr,2,axis=1)

# print the NumPy Array
print('Repeating 2-D Array Column Wise\n',new_arr)

Output

Original Array
 [[1 2 3]
 [4 5 6]]

Repeating 2-D Array Column Wise
 [[1 1 2 2 3 3]
 [4 4 5 5 6 6]]

Conclusion

The numpy.repeat() method takes no of repetitions (int) as input, repeats the elements of an array, and returns as output. By default, if we do not pass the axis parameter the 2-Dimensional array would be flattened into 1-Dimensional and then repeated.

If we have to repeat the Array row-wise then we need to pass 0 into the axis parameter and for repeating column-wise we need to pass 1 into the axis parameter.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Python Ascii()

Python ascii()

Table of Contents Hide ascii() Syntax ascii() Parametersascii() Return ValueExample 1: How ascii() method works?Example 2: ascii() vs print() The ascii() in Python is a built-in function that returns a printable…
View Post
Numpy Mean

NumPy mean()

Table of Contents Hide NumPy mean() SyntaxNumPy mean() ParametersNumPy mean() Return ValueExample 1: Using the numpy.mean() method on a flattened arrayExample 2: Compute the mean of the array column-wise using…
View Post
Python String Casefold()

Python String casefold()

Table of Contents Hide casefold() Syntaxcasefold() Parametercasefold() Return ValueDifference between casefold and lower in PythonExample 1: Convert string to lowercase using casefold() Example 2: Compare strings using casefold() Python String casefold() method…
View Post