# 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 ## numpy.average() Function

Table of Contents Hide SyntaxParametersReturn ValueRaisesExample 1: NumPy average() of Array values Example 2: NumPy average() of an array values in column-wise Example 3: NumPy average() of an array values in row-wiseExample… ## Python divmod()

Table of Contents Hide divmod() Syntax divmod() Parametersdivmod() Return ValueExample: How divmod() works in Python?Errors And Exceptions  The divmod() in Python is a built-in function that takes two numbers as input… ## Python Delete File – Step-by-Step Guide

Table of Contents Hide Methods to Delete Files in PythonMethod 1 – Using os ModuleDelete a fileDelete a directoryMethod 2 – Using shutil moduleMethod 3 – Using pathlib moduleRemove a… ## Python Print Variable

Table of Contents Hide How to use the print() function in Python?How to Print variable in Python?Method 1: Using comma , character to separate the variables in a print statementMethod… ## Python urllib.error.httperror: http error 403: forbidden

The urllib.error.httperror: http error 403: forbidden occurs when you try to scrap a webpage using urllib.request module and the mod_security blocks the request. There are several reasons why you get this error.… ## Python syntaxerror: eol while scanning string literal

Table of Contents Hide What does EOL while scanning string literal error mean in Python?Missing quotation marksString spanning multiple lines using the wrong syntaxMismatch of quotesUsing backslash before the ending…