Numpy has a function that should remind you of a tile floor. In fact, it’s called Numpy Tile. Np Tile is a pretty significant function that allows you to take a matrix and tile it as many times as you want. So let’s get into this cool Numpy Tile function in Python.
Numpy tile (np.tile) in Python simply repeats the numbers of elements present in an array. Suppose you have a numpy array [4,5,6,7], then np.tile([4,5,6,7], 3) will duplicate the elements three times and make a new array. We already know numpy always returns an array even if you give it a list.
Syntax of Numpy Tile
Here the parameter ‘A’ will be of array_like type. Parameter ‘A’ is used for taking the input as an array.
The reps parameter allows you to indicate how to repeat the input array. That includes the number of times to repeat it, but also the construction of this tiled output.
The tiled output array.
Examples of Numpy Tile Function
NumPy tile is not difficult to comprehend, but it may be a bit confusing at first. That is why we’re going, to begin with, straightforward examples and then gradually increase the sophistication. In the latter cases, you will start to get more of a feeling of how numpy.tile functions.
Example 1: Basic Numpy Tile Example
In this basic example we will simply multiply an array using the tile() function.
import numpy as np arr = np.array([0, 1, 2]) c = np.tile(arr, 2) print(c)
[0 1 2 0 1 2]
In the above straightforward example, firstly, we have imported the numpy. After that, we initialized and declared a numpy array with the values ‘[0, 1, 2]’. Subsequently, we used the np.tile() function or method to multiply the array. Here in this example, the parameters are ‘(arr, 2)’. So, the array will be repeated two times, as we can see in the output.
Example 2: Numpy Tile Vertically
In this case, we are going to have a simple 1-dimensional input and tile downwards. Basically, we are likely to see to the input just like a row of information and replicate the row.
import numpy as np arr = np.array([0, 1, 2]) reps = (3,1) c = np.tile(arr, reps) print(c)
[[0 1 2] [0 1 2] [0 1 2]]
In the above example, we have first imported the np module. After that, like example 1, we have initialized and assigned an array to the variable ‘arr.’ In this example, we have added an extra line ‘reps = (3,1)’. Here we are creating tile in the vertical direction. So, we need to make the column of the matrix as 1. And for the row, we can increase the value as much as required.
So, after formatting the rows and columns of the matrix, we simply used the function ‘np.tile()’. As we done in the example 1. After that, we simply printed the return value and got our desired result.
Example 3: Tiling Input Array Both Vertically And Horizontally
Let’s move to the third example, In this example we will tile a 1-d array to a matrix. The tile will repeat the elements of the array in both Horizontal and Vertical direction. This will also form a 1-d Matrix.
import numpy as np arr = np.array([0, 1, 2]) reps = (3,2) c = np.tile(arr, reps) print(c)
[[0 1 2 0 1 2] [0 1 2 0 1 2] [0 1 2 0 1 2]]
In the above example, we have first imported the required np module. After that, we declared and initialized a numpy array. Following the declaration, we stored the value ‘(3,2)’ in reps. Here we are creating tiles in the horizontal and vertical direction. So, we have taken the row = 3 and column = 2.
Subsequently, we passed the arguments array and reps in the np.tile function. At last, we printed the return value and got our result.
Example 4: Numpy Tile Example in 2-D Array
Enough of the 1-D array examples, in the last example we will work with the 2-D array. And how to operate with this type of array.
So, let’s directly move to the example.
import numpy as np arr = np.array([[0, 1], [2, 3]]) reps = (2,2) c = np.tile(arr, reps) print(c)
[[0 1 0 1] [2 3 2 3] [0 1 0 1] [2 3 2 3]]
In the above example, we have done the initial step similar to examples 1, 2, 3. But here, instead of initializing a 1-D array. We initialized a 2-D array with values [[0, 1], [2, 3]]. Following the initialization, we stored (2, 2) in the ‘reps‘ variable. Which will be our repetition parameter for np.tile() function. After that, we used the function and tiled the 2-d array according to our requirement.
Applications on Numpy Tile
- Can be useful when replicating an array n times and in n dimensions.
- Used Frequently in Mathematical and Scientific Calculations.
In conclusion, we can say the tile() function can be used to build an array by replicating the number of instances given by reps.
And we can use it for multidimensional arrays. In all honesty, it is a bit more complex to imagine and consider higher dimensions, therefore for the sake of simplicity, so I am not likely to describe much about it. Just realize that np.tile does operate in over two dimensions.
However, if you have any doubts or questions do let me know in the comment section below. I will try to help you as soon as possible.