# numpy.repeat() Function

The `numpy.repeat()` function takes the repetitions (int) as an input argument, repeats the elements of an array, and returns the repeated 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.

## Syntax

The syntax of `numpy.repeat()` function is:

``numpy.repeat(a, repeats, axis=None)``

## 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.

## 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()` function repeats the elements of an array along the given axis and returns the repeated array 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.

Reference: NumPy Library

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 String isalpha()

Python string isalpha() method is mainly used to check if the string is the alphabet or not. The isalpha() method returns true if all the characters in the string are… ## Calculate Euclidean Distance in Python

Table of Contents Hide How to Calculate Euclidean Distance in Python?Method 1: Using linalg.norm() Method in NumPyMethod 2: Using dot() and sqrt() methodsMethod 3: Using square() and sum() methodsMethod 4:… ## How To Convert Python String To Array

Table of Contents Hide Python String to Arraysplit() Syntaxsplit() ParametersExample 1: Split the string using the default argumentsExample 2: Split the string using the specific characterPython String to Array of… ## JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Table of Contents Hide JSONDecodeError: Expecting value: line 1 column 1 (char 0)Example JSONDecodeError: Expecting value: line 1 column 1 (char 0)Solution Other Possible SolutionsEnsure HTTP 200 status code and valid… ## [Solved] NameError: name ‘np’ is not defined

Table of Contents Hide Solution NameError: name ‘np’ is not defined Method 1 – Importing NumPy with Alias as npMethod 2 – Importing all the functions from NumPyMethod 3 –… ## How to Rename a file in Python?

Table of Contents Hide Python Rename FileExample to rename a file in PythonPython Rename Multiple FilesExample to Rename Multiple Files in Python The os module in Python comes in handy…