Numpy Tile Method Implementation in Python with Steps

Numpy Tile Method Implementation in Python

Numpy tile allows you to repeat the whole NumPy array in one dimension or two dimensions. There is also another function that allows you to repeat elements of the array. It is numpy.repeat(). But in case you want to repeat the whole array then the NumPy tile() method is used. In this entire tutorial, you will know how to implement numpy tile() method with examples.

 

Steps to implement NumPy tile function

Step 1: Import all the required libraries

Here in our example, we are using only the NumPy array so let’s import them using the import statement.

import numpy as np

Step 2: Create a dummy array

The second step is to create a sample array where you will implement the various examples. You can create a NumPy array using the numpy.array() method . I will use the tile() method both on the 1D array and 2D array. So let’s create them.

1D Numpy array

array_1d = np.array([10,20,30])

2D Numpy array

array_2d= np.array([[10,20,30],[40,50,60]])

 

Step 3: Apply the numpy tile method

After the creation of the NumPy array let’s apply the tile method. There are many ways to apply it.  Let’s explore all the examples.

Example 1: Repeating over columns

To repeat the array over the columns then you have to pass the value as the second argument inside the tile() method. Execute the below lines of code.

1D array

np.tile(array,2)

Output

repeating array over columns for 1d array
repeating array over columns for 1d array

2 D array

np.tile(array_2d,2)

Output

repeating array over columns for 2d array
repeating array over columns for 2d array

 

Example 2: Repeating array over Rows

To repeat the array over rows then you have to pass the tuple (no_of_rows, no_of_columns)  as an argument to the tile() method. For example, I want to repeat the array for 2 rows then I will execute the following code.

1 D array

np.tile(array_1d,(2,1))

Output

repeating array over rows for 1d array
repeating array over rows for 1d array

2D Array

np.tile(array_2d,(2,1))

Output

repeating array over rows for 2d array
repeating array over rows for 2d array

Example 2: Repeating array over Rows and columns

In the above two examples, you have repeated arrays over columns or rows. Now let’s repeat the array in both rows and columns. To do so you have to pass both the columns and rows as an argument of the tile() method.

1D array

np.tile(array_1d,(2,3))

Output

repeating array over rows and columns for 1d array
repeating array over rows and columns for 1d array

2D array

np.tile(array_2d,(2,3))

Output

repeating array over rows and columns for 2d array
repeating array over rows and columns for 2d array

Conclusion

These are the examples on the NumPy tile that I have aggregated for you. Numpy tile() allows you to easily repeat your NumPy arrays along with columns or rows. I hope this tutorial has cleared your all queries on tile() method. Even if you have doubt then you can contact us for more help.

Source:

Numpy Tile Documentation

 

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner