The tile() method constructs an array by repeating arrays.
tile() Syntax
The syntax of tile() is:
numpy.tile(array, repetitions)
tile() Arguments
The tile() method takes two arguments:
array- an array with elements to repeatrepetitions- number of times the array repeats
tile() Return Value
The tile() method returns a new array with repetition of input array.
Example 1: Tile a 1-D Array
Output
1-D tile: [0 1 0 1 0 1] 2-D tile: [[0 1 0 1 0 1] [0 1 0 1 0 1]]
In the line
array3 = np.tile(array1,(2, 3))
the tuple (2, 3) indicates that array1 is repeated 2 times along axis 0 and 3 times along axis 1. This results in a 2-dimensional array with the repeated pattern.
Note: np.tile(array1, 0) removes all elements from an array.
Example 2: Tile a 2-D Array
We can tile a 2-D array similar to a 1-D array.
Output
Tile with 2 repetitions: [[0 1 2 0 1 2] [3 4 5 3 4 5]] Tile with (2,3) repetitions: [[0 1 2 0 1 2 0 1 2] [3 4 5 3 4 5 3 4 5] [0 1 2 0 1 2 0 1 2] [3 4 5 3 4 5 3 4 5]] Tile to make a 3-D array: [[[0 1 2 0 1 2] [3 4 5 3 4 5] [0 1 2 0 1 2] [3 4 5 3 4 5]] [[0 1 2 0 1 2] [3 4 5 3 4 5] [0 1 2 0 1 2] [3 4 5 3 4 5]]]
Note: np.tile() is similar to np.repeat(), with the main difference being that tile() repeats arrays whereas repeat() repeats individual elements of the array.