In this tutorial, you’ll learn how to use the NumPy tile function to arrange arrays using Python. The NumPy
tile() function takes an array as an input and creates a new array by repeating the input array in different ways. Being able to work with and manipulate NumPy arrays is an important skill for data analysis, machine learning, and deep learning.
By the end of this tutorial, you’ll have learned:
- What the NumPy tile function does
- How to arrange NumPy arrays horizontally (column-wise)
- How to arrange NumPy arrays vertically (row-wise)
- How to arrange multi-dimensional arrays using NumPy tile
Let’s get started!
Table of Contents
NumPy Tile Function Syntax
tile() function takes an array,
A, and returns the array repeated by a given number of repetitions. The function can work quite simply by passing in an integer to simply repeat length-wise. However, you can also tile the arrays using in two dimensions, or even more dimensions.
Let’s take a look at how the function is constructed:
# Understanding the np.tile() Function np.tile( A, # The input array reps # Num of repetitions along each axis )
Let’s break this down a little further. The parameter,
A, allows you to pass in a NumPy array or an array-like data structure, such as a list or a tuple.
reps= parameter is a bit more complex. This is because the
reps= parameter can be used to change the dimensions of the array. The argument you pass into the
reps parameter is an array-like data structure which defines the shape in which you want to tile the array.
For example, when you pass in a tuple of
(1, 2), NumPy will tile the original array one time along the first dimension and twice along the second dimension.
Let’s see how we can put the NumPy
tile() function to use!
How to Arrange NumPy Arrays Horizontally with Tile
In this section, you’ll learn how to use the NumPy
tile() function to repeat items horizontally. Let’s first create a NumPy array and then see how we can pass it into the
tile() function to repeat it. In our example, we’ll repeat the example 3 times:
# Repeating a NumPy Array Horizontally with tile() import numpy as np array1 = np.array([1,2,3]) tiled = np.tile(array1, 3) print(tiled) # Returns: # [1 2 3 1 2 3 1 2 3]
We can see in the example above that a single, one-dimensional array is returned. Note here that the dimension didn’t change! We did not repeat the array three times, but rather the items in the array.
In the next section, you’ll learn how to arrange NumPy arrays vertically.
How to Arrange NumPy Arrays Vertically with Tile
Similar to the example above, we can also use NumPy to arrange arrays vertically. In this case, we can stack the arrays on top of one another. Because we’ll be turning a one-dimensional array into two dimensions, the dimensions will change
In order to do this, we need to pass in a tuple with two dimensions. Imagine we wanted to repeat the array three time, column-wise. In that case, we could write:
# Repeating a NumPy Array Vertically with tile() import numpy as np array1 = np.array([1,2,3]) tiled = np.tile(array1, (3,1)) print(tiled) # Returns: # [[1 2 3] # [1 2 3] # [1 2 3]]
What we’ve done here is pass in the tuple of
(3, 1), which means the array is repeated three times down and only a single time across.
Let’s try something different, where we can repeat the array both horizontally and vertically. Let’s pass in the tuple
(3, 2), meaning that the array should repeat three times down and two times across:
# Repeating an Array Both Across and Down import numpy as np array1 = np.array([1,2,3]) tiled = np.tile(array1, (3,2)) print(tiled) # Returns: # [[1 2 3 1 2 3] # [1 2 3 1 2 3] # [1 2 3 1 2 3]]
How to Arrange NumPy Multidimensional Arrays with Tile
In this section, you’ll learn how to arrange multi-dimensional arrays using the
np.tile() function. This process works exactly the same as the previous examples, except the arranging of the tiles will look a little differently.
Let’s take a look at an example of a 2×2 array which we will tile twice in each direction:
# Arranging Multidimensional Arrays with .tile() import numpy as np array1 = np.array([[1,2], [3,4]]) tiled = np.tile(array1, (3,2)) print(tiled) # Returns: # [[1 2 1 2] # [3 4 3 4] # [1 2 1 2] # [3 4 3 4] # [1 2 1 2] # [3 4 3 4]]
How to Repeat Python Lists and Tuples with NumPy Tile
The NumPy tile function expects an array-like structure. This means that we can actually pass in lists and tuples and the items will also be repeated. Let’s see how we can pass in a tuple and have it be repeated with the
# Tiling a Tuple with np.tile() import numpy as np tuple1 = (1,2,3) tiled = np.tile(tuple1, (3,2)) print(tiled) # Returns: # [[1 2 3 1 2 3] # [1 2 3 1 2 3] # [1 2 3 1 2 3]]
Even though we passed in a tuple, the function still returns a NumPy array.
In this tutorial, you learned how to use the NumPy tile function. You learned how the syntax of the function allows you to pass in an array-like structure and the pattern in which you want to arrange the data. Then, you learned how to arrange arrays horizontally and vertically. You also learned how to work with multi-dimensional arrays and how to tile lists and tuples.
To learn more about related topics, check out the tutorials below: