Introduction to NumPy Tile
Python provides different functions to the users. To work with arrays, the python library provides a numpy tile. The numpy tile provides a facility to create a new array by repeating a number of times as per the repetition. After execution of the tile function, we get a new array that has dimension max(A, ND, R) where R is the length of repetition. If A.ND > R is promoted to A.ND by prepending 1’s to it. If A.ND<R is promoted to A.ND prepending a new axis. Arrays play a major role in data science where speed matters. Numpy is an acronym for numerical python. Basically, numpy is an open-source project.
Syntax
numpy.tile(A,R)
Explanation
In the above syntax where numpy.tile is the function, where A is the input of array and this element must be required. R is representing the number of repetitions with array (A) along each axis and this element must be required.
How does tile function work in NumPy?
- We must install Python on your system.
- We must install numpy using the pip command.
- We required basic knowledge about Python.
- We required basic knowledge about tile.
- We can perform different operations using a numpy.tile function.
Examples of NumPy Tile
Let’s see a different example of a numpy tile.
Example #1 – D Array
Code:
import numpy as np
array = np.arange(4)
print("arr : \n", array)
repetitions = 3
print("Repeating array 3 times : \n", np.tile(array, repetitions))
repetitions = 4
print("\nRepeating array 4 times : \n", np.tile(array, repetitions))
Explanation:
In the above example, we implement numpy.tile function with a 1D array. In this example, we first import the numpy function and assign it as np. Then we declare a variable array and assign array values 4 with arange function. After that, we simply try to print array values. Then we declare two repetition values that are 3 and 4 with the help of the np.tile function and finally, we try to print the given array with repetitions. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example #2 – Normal Example of numpy tile
Code:
import numpy as np
A = np.array([0, 1, 2])
print(A)
x=np.tile(A, reps=3)
print(x)
Explanation:
In the above example, we import the numpy function and then assign it as np. Then we have an array with an axis value of 0 and repeat three times. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example #3 – A.ND is less than R
Code:
import numpy as np
A = np.array([1, 2, 3])
print(A)
print("repeated array....")
x=np.tile(A, reps=(3, 3))
print(x)
Explanation:
In the above example, we try to implement the first case that the array dimension is less than repetition. First, we import the numpy function and assign it as np. Then we declare an array with values and assign repetition values. Finally, the result has been printed. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example #4 – A.ND is greater than R
Code:
import numpy as np
A = np.array([[2, 3], [4, 5]])
print(A)
print("repeated array....")
x=np.tile(A, reps=3)
print(x)
Explanation:
In the above example, we have implemented another special case that is A.ND is > than R. In which first we import numpy functions and use them as np. Then we declare an array with values and assign repetition values. Finally, we tried to print the result. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example #5 – for (A.ND==R) == 0
Code:
import numpy as np
A = np.arange(4).reshape(2, 2)
print("Array : \n", A)
x = 2
y = 1
repetitions = (x, y)
print("\n Repeated array : \n", np.tile(A, repetitions))
print("Array Shape : \n", np.tile(A, repetitions).shape)
x = 3
y = 2
repetitions = (x, y)
print("\n Repeated array : \n", np.tile(A, repetitions))
print("Array Shape : \n", np.tile(A, repetitions).shape)
x = 2
y = 3
repetitions = (x, y)
print("\n Repeated array : \n", np.tile(A, repetitions))
print("Array Shape : \n", np.tile(A, repetitions).shape)
Explanation:
In the above example, we implement a special case (A.ND==R)== 0 condition in which we first import numpy functions and use them as np. Then we declared an array with values and assigned different repetition values in the program. Finally, we tried to print the array with shape. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example #6 – for different methods to write tile function
Code:
import numpy as np
matrix= np.array([[1,2],[3,4]])
x = np.tile(matrix,4)
y = np.tile(matrix,(1,4))
print(x)
print(y)
Explanation:
In the above example, we implement a tile function by using two different methods. The resultant array is the same after the execution of both methods. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example #7 – For Horizontal +Vertical implementation of tile function
Code:
import numpy as np
matrix= np.array([[2,3],[4,5]])
x = np.tile(matrix,(3,4))
print(x)
Explanation:
In the above example, we implement a tile function with a horizontal and vertical matrix. Illustrate the end result of the above declaration by using the use of the following snapshot.
Example #8
Code:
import numpy as np
a = np.arange(3)
print("array : \n", a)
x = 3
y = 23
rep = (x, y)
print("\n Repeated array : \n", np.tile(a, rep))
print("Array Shape : \n", np.tile(a, rep).shape)
x = 3
y = 2
rep = (x, y)
print("\n Repeated array : \n", np.tile(a, rep))
print("Array Shape : \n", np.tile(a, rep).shape)
x = 2
y = 3
rep = (x, y)
print("\n Repeating array : \n", np.tile(a, rep))
print("Array Shape : \n", np.tile(a, rep).shape)
Explanation:
In the above example, we import a numpy function and then assign it as np. Then we have created an array by arange function. After that, we assigned repetition values to the array by using numpy.tile function then we tried to print array with the shape of the array. The same process we follow repeatedly for different repetition values. Illustrate the end result of the above declaration by using the use of the following snapshot.
Conclusion
We hope from this article you have understood about the numpy tile function. From the above article, we have learned basic syntax numpy tile function. We have also learned how we can implement them in python with different examples of each type. From this article, we have learned how we can handle numpy tile in python.
Recommended Articles
This is a guide to NumPy Tile. Here we discuss How does tile function works in NumPy and Examples along with the codes and outputs. You may also look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses