The python numpy tile function repeats the array elements for a given number of times and constructs a new array. This method will consider the input array as a tile or piece and repeat for a given number of times vertically and horizontally.
numpy.tile(arr, repetition)
This Python numpy tile method will return a 1D, 2D, 3D or multi-dimensional array based on the d value or length of a given ndarray. To understand better, consider the length of a given array as d.
- If repetition is a single scalar value, arr will repeat for d times, and the output dimension = max(d, arr.ndim). For instance, if arr is one-dimensional, the output will be 1D, if arr is two-dimensional, the output will be 2D, etc.
- If arr.ndim < d, the output dimension = d.
- If arr.ndim > d, the output dimension = arr.ndim.
Python NumPy tile Example
In this program, we declared an array of three numbers. For the b variable, the repetition value is 2, so 1, 2, and 3 will repeat twice. The next one will repeat thrice.
import numpy as np a = np.array([1, 2, 3]) b = np.tile(a, 2) print(b) c = np.tile(a, 3) print(c)
[1 2 3 1 2 3]
[1 2 3 1 2 3 1 2 3]
Example 2
In this example, we use the multiple values as the repetition argument. So, the below examples will convert the one-dimensional array to 2D and 3D.
import numpy as np a = np.array([1, 2, 3, 4]) b = np.tile(a, (2, 2)) print(b) c = np.tile(a, (3, 2)) print(c) d = np.tile(a, (4, 1)) print(d) e = np.tile(a, (2, 1, 3)) print(e)
[[1 2 3 4 1 2 3 4]
[1 2 3 4 1 2 3 4]]
[[1 2 3 4 1 2 3 4]
[1 2 3 4 1 2 3 4]
[1 2 3 4 1 2 3 4]]
[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]]
[[[1 2 3 4 1 2 3 4 1 2 3 4]]
[[1 2 3 4 1 2 3 4 1 2 3 4]]]
np.tile(a, (3, 2))
d = len(reps) = 2
ndim = 1
max(2, 1) = 2 = d, so it returns the array of 3 columns, and arr values will repeat two times.
np.tile(a, (2, 1, 3)) means block size = 2, and each dimension has single dimensional array. The ndarray values in each dimension will repeat three times.
Python numpy two Dimensional tile
For two-dimensional array inputs, consider the 2D array as a single item.
a, (3, 2) = The whole 2D array will repeat vertically 3 times and horizontally two times.
np.tile(a, (2, 3, 4)) = 2D array will convert to 3D where block size = 2. On each block, the 2D array (a) will repeat vertically three times and horizontally four times.
import numpy as np a = np.arange(4).reshape(2, 2) print(a) b = np.tile(a, (3, 2)) print(b) c = np.tile(a, (2, 2, 3)) print(c) d = np.tile(a, (2, 3, 4)) print(d)
[[0 1]
[2 3]]
[[0 1 0 1]
[2 3 2 3]
[0 1 0 1]
[2 3 2 3]
[0 1 0 1]
[2 3 2 3]]
[[[0 1 0 1 0 1]
[2 3 2 3 2 3]
[0 1 0 1 0 1]
[2 3 2 3 2 3]]
[[0 1 0 1 0 1]
[2 3 2 3 2 3]
[0 1 0 1 0 1]
[2 3 2 3 2 3]]]
[[[0 1 0 1 0 1 0 1]
[2 3 2 3 2 3 2 3]
[0 1 0 1 0 1 0 1]
[2 3 2 3 2 3 2 3]
[0 1 0 1 0 1 0 1]
[2 3 2 3 2 3 2 3]]
[[0 1 0 1 0 1 0 1]
[2 3 2 3 2 3 2 3]
[0 1 0 1 0 1 0 1]
[2 3 2 3 2 3 2 3]
[0 1 0 1 0 1 0 1]
[2 3 2 3 2 3 2 3]]]
The next two examples will show you how to tile the numpy array horizontally and vertically.
tile Horizontally
import numpy as np a = np.arange(5) b = np.tile(a, (3, 1)) print(b) c = np.arange(4).reshape(2, 2) print(c) d = np.tile(c, (4, 1)) print(d)
[[0 1 2 3 4]
[0 1 2 3 4]
[0 1 2 3 4]]
[[0 1]
[2 3]]
[[0 1]
[2 3]
[0 1]
[2 3]
[0 1]
[2 3]
[0 1]
[2 3]]
tile Vertically
import numpy as np a = np.arange(5) b = np.tile(a, (4)) print(b) c = np.arange(4).reshape(2, 2) print(c) d = np.tile(c, (1, 4)) print(d)
[0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]
[[0 1]
[2 3]]
[[0 1 0 1 0 1 0 1]
[2 3 2 3 2 3 2 3]]