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.

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. For more examples on Python NumPy refer to NumPy Tutorial.

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.


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

#  Example 4: 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 5: Repeat the elements of the 2-D array with axis = 1
arr2 = np.repeat(arr,2,axis=1)

#  Example 6: Repeat the elements of the 2-D array 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 – 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 of repeat()

  • 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 specified number of times given by the repeats parameter. Lets create the NumPy array using numpy.array() function.


import numpy as np
# create 1-D array
arr = np.array([5,7,9,11,13,19])

# Repeat elements of an array 
arr2 = np.repeat(arr, 2)
print(arr2)

# Output
[ 5  5  7  7  9  9 11 11 13 13 19 19]

4. Repeat 2- D NumPy Array Elements

As I mentioned above, this function allows repeating elements of any dimensions of an array and it returns flattened array by default. Take 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]])

# Repeat the elements of 2-D array
arr2 = np.repeat(arr, 2)
print(arr2)

# Output
[15 15 28 28 57 57 99 99 65 65 34 34 37 37 55 55 88 88]

5. Repeat Array Elements along with Axis = 1

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. For example,


# Repeat the elements of 2-D array with axis = 1 
arr2 = np.repeat(arr,2,axis=1)
print(arr2)

# Output
[[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 specify the axis=0 to the repeat() function. It will return the exact shape of an array as repeated elements vertically. For example,


# Repeat the elements of 2-D array with axis = 0
arr2 = np.repeat(arr,2,axis=0)
print(arr2)

# Output
[[15 28 57]
 [15 28 57]
 [99 65 34]
 [99 65 34]
 [37 55 88]
 [37 55 88]]

7. Conclusion

In this article, I have explained NumPy repeat() function and using this how we can get the array of repeated elements along with specified axis with examples.

You May Also Like

References

Leave a Reply

You are currently viewing Python NumPy repeat() Function