What Is the Tile Function in NumPy?
A tile is a thin rectangular slab made of concrete, which is used to cover roofs, floors, etc. When we congregate two or more tiles in the same way, we get a tile floor.
The tile function in NumPy is a function that repeats the input array an arbitrary number of times. The resulting array has the same dimensions as the input array.
Syntax of NumPy Tile
The syntax for the tile function in NumPy is:
Parameters
The parameters for the np tile function in NumPy are:
- arr: Input array.
- repetitions: The number of repetitions we want for our array.
Examples of NumPy Tile Function
After understanding the syntax and various parameters of the np tile function in NumPy, let's dive into some examples to make our concepts better:
-
Basic Example
Output
As we can see, our input array ([0, 1, 2, 3]) has been repeated twice after using the tile() function.
-
NumPy Tile Vertically
Just as we horizontally repeat an array, we can repeat an array vertically too!
Output
With the help of changing the repetitions, we can repeat an array vertically. We have added coordinates to the repetitions variable because we need to make the column of the matrix 1. There are no such limits on rows.
-
Tiling Input Array Both Vertically And Horizontally
In this section, we will see a method to tile a one-dimensional array to a matrix. To achieve this, we need to make some minor changes to the code for vertical tiling.
Output
Hence, we have obtained a two-dimensional matrix from a one-dimensional array using horizontal and vertical tiling.
-
Numpy Tile Example in 2-D Array
In this example, we will convert a one-dimensional array into its tile equivalent in a two-dimensional array. We will try to create a square matrix (A matrix having equal rows and columns is termed a square matrix).
Output
Hence, we converted our one-dimensional array ([2, 4, 6, 8]) into its tile equivalent in a square matrix.
Applications of NumPy Tile Function
Like other NumPy array manipulation functions, the np Tile() function has advantages that we can use to ease our life as developers.
The np Tile() function in NumPy can be used to replicate an array n times and in n dimensions. This gives us complete control over our arrays, which results in more efficiency while coding.
Testing is a very important step in a software lifecycle, and many times we get stuck when we create new test data for our prototype. With the help of the tile function, we can generate seemingly related arrays but have repetitions in them.
The np Tile() in NumPy is one of the most frequently used functions in calculations of Mathematics and Applied Sciences. To create variations, eigenvalues and eigenvectors include the use of the tile() function in Python.
Conclusion
- The np Tile() function in NumPy helps us to create an array by simply replicating the number of repetitions by a variable.
- We understood the syntax and parameters of the np tile() function, along with the various changes we made to get our desired replicated array.
- Apart from this, we also saw a few types of replicated arrays; Vertical tiling, vertical and horizontal tiling, and tiling in a two-dimensional matrix.