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