• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:16 mins read
You are currently viewing Python NumPy repeat() Function

Python NumPy repeat() function is used to repeat the individual elements of an array a specified number of times. Specify the number of times to repeat by the repeats parameter. It allows repeating elements of any dimensional array for example single and multi-dimensional arrays. It returns the repeated elements of a flattened array by default. Using tile() we can repeat the given array a specified number of times.

Advertisements

In this article, I will explain the repeat() function by using syntax and parameters, and how to repeat the individual elements of the given array with examples.

1. Quick Examples of NumPy repeat() Function

If you are in a hurry, below are some quick examples of how to use Python numpy.repeat() function.


# Quick examples of numpy repeat() function

# Example 1: Repeat the elements of array 
arr = np.array([5,7,9,11,13,19])
arr2 = np.repeat(arr, 2)

# Example 2: Repeat the elements of the 2-D array
arr = np.array([[15,28,57],[99,65,34],[37,55,88]])
arr2 = np.repeat(arr, 2)

# Example 3: Repeat the elements of the 2-D array 
# With axis = 1
arr2 = np.repeat(arr,2,axis=1)

# Example 4: Repeat the elements of the 2-D array 
# With axis = 0 
arr2 = np.repeat(arr,2,axis=0)

2. Syntax of NumPy repeat() Function

Following is the syntax of the repeat() function.


# Syntax of repeat()
numpy.repeat(arr, repeats, axis=None)

2.1 Parameter of repeat()

Following are the parameters of the repeat().

  • arr – This is the input array.
  • repeats – Int or array of int type. Defines the number of repetitions for each element. repeats are broadcasted to fit the shape of the given axis.
  • axis – Which values are repeated along with the specified axis. By default, it takes the flattened input array and returns a flattened output array. It can be 0 and 1.
  • Returns value – Output array which has the same shape as arr, except along the given axis.

2.2 Return Value

  • It returns an ndarray.
  • For single dimensional array it returns the same shape.
  • For multi-dimensional arrays as input, it returns a flattened array by default. By using axis param it can return the array of the same shape as the input.

3. Usage of NumPy repeat() Function

repeat() function from NumPy allows to repeat of the individual elements of an array along with the given repeat parameter. It returns an array of multiple elements with the same shape as the given axis as the input array.

Use this function to construct an array by repeating elements of a 1-D array a specified number of times given by the repeats parameter. Let’s create the NumPy array using numpy.array() function.


# Import numpy
import numpy as np

# Create 1-D array
arr = np.array([5,7,9,11,13,19])
print("Original array:\n", arr)

# Repeat elements of an array 
arr2 = np.repeat(arr, 2)
print("After repeating elements of an array:\n",arr2)

Yields below output.

numpy repeat

As you can see, each element of the original array is repeated twice, resulting in a new array with repeated elements.

4. Repeat 2- D NumPy Array Elements

You can use the numpy.repeat() function to repeat the elements of a 2D array. In this code, the numpy.repeat() function is applied to the 2D array arr. Each element of the original array is repeated twice. As a result, each element in the original 2D array is repeated twice, and the resulting array arr2 contains the repeated elements.

As I mentioned above, this function allows repeating elements of any dimensions of an array and it returns a flattened array by default. Take a 2-D array and repeat the elements using this function. For example,


# Create 2-D array
arr = np.array([[15,28,57],[99,65,34],[37,55,88]])
print("Original array:\n", arr)

# Repeat the elements of 2-D array
arr2 = np.repeat(arr, 2)
print("After repeating elements of an 2D array:\n",arr2)

Yields below output.

numpy repeat

5. Repeat Array Elements along with Axis = 1

To repeat array elements along a specific axis in a 2D array, you can use the numpy.repeat() function with the axis parameter set to 1. For example, the numpy.repeat() function is used with axis=1. Each element of the original array is repeated twice along the columns (axis=1). As you can see, each element in the original 2D array is repeated twice along the columns (axis=1) in the resulting array arr2.

To get the same shape of an array repeat the elements of a 2-D array using this function along with a specified axis. When you use axis=1, the elements will be repeated along with columns or horizontally.


import numpy as np

# Create a 2-D array
arr = np.array([[15, 28, 57], [99, 65, 34], [37, 55, 88]])
print("Original array:\n", arr)

# Repeat the elements of 2-D array with axis=1
arr2 = np.repeat(arr, 2, axis=1)
print("After repeating elements along axis 1 in a 2D array:\n", arr2)

# Output:
# Original array:
#  [[15 28 57]
#  [99 65 34]
#  [37 55 88]]
# After repeating elements along axis 1 in a 2D array:
#  [[15 15 28 28 57 57]
#  [99 99 65 65 34 34]
#  [37 37 55 55 88 88]]

6. Repeat Array Elements along with Axis = 0

You can get repeated elements of an array vertically or row-wise along a specific axis in a 2D array along the rows (axis=0), you can use the numpy.repeat() function with the axis parameter set to 0. For instance, the numpy.repeat() function is used with axis=0. Each row of the original array is repeated twice along the rows (axis=0). It will return the exact shape of an array as repeated elements vertically.


import numpy as np

# Create a 2-D array
arr = np.array([[15, 28, 57], [99, 65, 34], [37, 55, 88]])

# Repeat the elements of 2-D array 
# Along with axis=0
arr2 = np.repeat(arr, 2, axis=0)
print("After repeating elements along axis 0 in a 2D array:\n", arr2)

# Output:
# After repeating elements along axis 0 in a 2D array:
# [[15 28 57]
# [15 28 57]
# [99 65 34]
# [99 65 34]
# [37 55 88]
# [37 55 88]]

Frequently Asked Questions

What does the numpy.repeat() function do in Python NumPy?

The numpy.repeat() function is used to repeat elements of an array along a specified axis. It can repeat individual elements a specific number of times or repeat elements along a particular axis of the array.

How can I repeat each element of a 1D array multiple times using numpy.repeat()?

You can repeat each element of a 1D array multiple times using the numpy.repeat() function. For instance, the numpy.repeat() function is used to repeat each element of the input array [1, 2, 3] three times. The repeats variable specifies the number of times each element should be repeated. You can adjust the value of repeats to repeat the elements a different number of times.

How can I repeat elements along a specific axis in a 2D array using numpy.repeat()?

You can repeat elements along a specific axis in a 2D array using the numpy.repeat() function by specifying the axis parameter. For example, the numpy.repeat() function is used to repeat each element of the input 2D array [[1, 2], [3, 4]] three times along the columns (axis=1).

Can I specify different repetition factors for each element in the input array using numpy.repeat()?

You can specify different repetition factors for each element in the input array using the numpy.repeat() function. To achieve this, you can pass an array of integers as the repeats parameter. Each element in the repeats array specifies the number of times the corresponding element in the input array should be repeated.

What happens if I don’t specify the axis parameter in numpy.repeat() function?

If you don’t specify the axis parameter in the numpy.repeat() function, the input array is flattened before use, and the elements are repeated as if it’s a 1D array. Essentially, the function treats the input array as a 1D sequence of elements, and each element is repeated based on the specified number of repetitions.

Conclusion

In this article, I have explained the NumPy repeat() method syntax, parameter, and usage of how you can get the array of repeated elements along with specified axis with examples.

References