numpy.tile() in Python
numpy.tile() in Python
The numpy.tile() function constructs an array by repeating the parameter ‘A’ the number of times as specified by the ‘reps’ parameter.
Syntax
numpy.tile(A, reps)
Parameter
The numpy.tile() function consists of two parameters, which are as follows:
A: This parameter represents the input array.
reps: This parameter represents the number of repetitions of A along each axis.
Return
This function returns the tiled output array.
Example 1
# Python Program explaining # numpy.tile() function import numpy as np #Working on 1D A = np.arange(4) print("arr value: \n", A) rep = 3 print("Repeating A 3 times: \n", np.tile(A, rep)) rep = 5 print("\nRepeating A 5 times: \n", np.tile(A, rep))
Output
arr value: [0 1 2 3] Repeating A 3 times: [0 1 2 3 0 1 2 3 0 1 2 3] Repeating A 5 times: [0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3]
Example 2
# Python Program explaining # numpy.tile() function import numpy as np arr = np.arange(4).reshape(2, 2) print("arr value: \n", arr) val1 = 2 val2 = 1 rep = (val1, val2) print("\nRepeating arr: \n", np.tile(arr, rep)) print("arr Shape : \n", np.tile(arr, rep).shape) val1 = 2 val2 = 3 rep = (val1, val2) print("\nRepeating arr : \n", np.tile(arr, rep)) print("arr Shape : \n", np.tile(arr, rep).shape)
Output
arr value: [[0 1] [2 3]] Repeating arr : [[0 1] [2 3] [0 1] [2 3]] arr Shape : (4, 2) Repeating arr : [[0 1 0 1 0 1] [2 3 2 3 2 3] [0 1 0 1 0 1] [2 3 2 3 2 3]] arr Shape : (4, 6)