# numpy.repeat() – The NumPy repeat Python Function

## Introduction to NumPy repeat

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 element 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 an array – arr elements as per repetitions, number of times we want to repeat the arr

## Code Examples of NumPy repeat

Example 01: Working on a 1D array

``````# welcome to softhunt.net
# Python Program illustrating
# numpy.repeat()

import numpy as np

#Working on 1D
arr = np.arange(5)
print("arr : \n", arr)

repetitions = 2
a = np.repeat(arr, repetitions)
print("\nRepeating arr 2 times : \n", a)
print("Shape : ", a.shape)

repetitions = 3
a = np.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 1 1 1 2 2 2 3 3 3 4 4 4]
Shape :  (15,)```

Example 02:

``````# welcome to softhunt.net
# Python Program illustrating
# numpy.repeat()

import numpy as np

arr = np.arange(6).reshape(2, 3)
print("arr : \n", arr)

repetitions = 2
print("\nRepeating arr : \n", np.repeat(arr, repetitions, 1))
print("arr Shape : \n", np.repeat(arr, repetitions).shape)

repetitions = 2
print("\nRepeating arr : \n", np.repeat(arr, repetitions, 0))
print("arr Shape : \n", np.repeat(arr, repetitions).shape)

repetitions = 3
print("\nRepeating arr : \n", np.repeat(arr, repetitions, 1))
print("arr Shape : \n", np.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 1 1 1 2 2 2]
[3 3 3 4 4 4 5 5 5]]
arr Shape :
(18,)```

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

## FAQs

### What is the Difference Between np.repeat and np.tile

The np.repeat function repeats the individual elements of an input array.

But np.tile will take the entire array – including the order of the individual elements – and copy it in a particular direction.

So the difference is between copying the individual numbers versus copying the whole array all at once.

### How can I Duplicate 1 Dimensional array?

Duplicating a 1D array is a little harder because a 1-dimensional array only has one axis (axis 0). This is a problem because when we duplicate the rows, there needs to be another dimension in which to copy the rows. So essentially, to do this, we actually need to increase the number of dimensions of the array first, and then copy the row.

Let’s take a look.

``````# welcome to softhunt.net
# Python Program illustrating
# numpy.repeat()

import numpy as np

np_array_1d = np.array([1,2,3,4])
np_array_2d = np.expand_dims(np_array_1d, axis = 0)
ans = np.repeat(a = np_array_2d, repeats = 2, axis = 0)
print(ans)``````

Output:

```[[1 2 3 4]
[1 2 3 4]]```

Note: These programs will not run in online IDEs. Please test them on your systems to see how they operate.

## Conclusion

That’s all for this article, if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn. And make sure you check out our NumPy tutorials.

Suggested Articles:

1. numpy.tile() – The NumPy tile Python Function