NumPy tile() function in Python is used to construct an array by repeating a given input array by specified number of times given by the reps
parameter. The NumPy np.tile()
function takes an array as an input and creates a new array by repeating the input array. This can be useful when you want to create a larger array by tiling or repeating a smaller array.
In this article, I will explain how to use the NumPy tile()
function and using it how to repeat a given array in different ways with examples.
1. Quick Examples of NumPy tile() Function
If you are in a hurry, below are some quick examples of how to use NumPy tile() function in Python.
# Quick examples of numpy tile() function
# Example 1: Use tile() function
# On numpy 1-d array
arr2 = np.tile(arr, 2)
# Example 2: Using tupe for reps param
repetitions = (2, 1)
arr2 = np.tile(arr, repetitions)
# Example 3: Use tile() function
# To repeat the 1-D array vertically
arr2 = np.tile(arr, (2, 1))
# Example 4: Use tile() function
# To repeat the 1-D array horizontally
repetitions = (1, 3)
arr2 = np.tile(arr, repetitions)
# Example 5: Use numpy.tile() function
# With two-dimenstional array
repetitions = (3,2)
arr2 = np.tile(arr, repetitions)
# Example 6: Use multi-dimensional arrays
arr2 = np.tile(arr, (2, 3))
2. Syntax of NumPy tile()
Following is the syntax of the numpy.tile() function.
# Syntax of numpy.tile()
numpy.tile(arr, reps)
2.1 Parameters of tile()
Following is the parameters of tile().
arr:
This is the input array that you want to repeat.reps:
This is the number of repetitions along each axis. It can be an integer or a tuple of integers. Ifreps
is an integer, it specifies the number of repetitions for all axes. Ifreps
is a tuple, it specifies the number of repetitions along each axis.
2.2 Return value of tile()
This function returns the tiled output array.
3. Usage numpy.tile() Function
Numpy tile()
function returns a new array, which contains a repeated input array in simple words it generates a new array from the original array along with specified repetitions.
You can repeat the given array using np.tile()
function along with specified repetitions (number of times to repeat). You can use the tile()
function to repeat elements of a 1-D array. For instance, the original 1-D array [2, 5, 8]
is repeated two times using the np.tile()
function, resulting in the following output.
# Import numpy
import numpy as np
# Create input array
arr = np.array([2, 5, 8])
print("Original array:", arr)
# Use tile() function to 1-D array
arr2 = np.tile(arr, reps=2)
print("Array after tiling:",arr2)
Yields below output.
Each element of the original array is repeated two times in the resulting array. You can adjust the second argument of np.tile()
to control the number of repetitions.
5. Use tile() Function to Vertically
To use the tile()
function to repeat a 1-D array vertically, you can create a 2-D array where the 1-D array is repeated along the vertical axis.
In the below example, the np.tile()
function is used to create a 2-D array by repeating the 1-D array [2, 5, 8]
three times along the vertical axis (axis=0
) and once along the horizontal axis (axis=1
).
# Use tile() function
# To repeat the 1-D array vertically
arr2 = np.tile(arr, (2, 1))
print("Array after tiling vertically:\n",arr2)
# Output:
# Array after tiling vertically:
# [[2 5 8]
# [2 5 8]]
6. Use tile() Function to Horizontally
If you want to use the tile()
function to repeat a 1-D array horizontally, you can create a 2-D array where the 1-D array is repeated along the horizontal axis.
In the below example, the np.tile()
function is used to create a 2-D array by repeating the 1-D array [2, 5, 8]
once along the vertical axis (axis=0
) and three times along the horizontal axis (axis=1
). This results in the following output:
# Use tile() function
# To repeat the 1-D array horizontally
repetitions = (1, 3)
arr2 = np.tile(arr, repetitions)
print("Array after tiling horizontally:\n",arr2)
# OutPut:
# Array after tiling horizontally:
# [[2 5 8 2 5 8 2 5 8]]
7. Use NumPy tile() Function to Two-Dimensional Array
Let’s use a two-dimensional array and apply numpy.tile()
function which will get repetitions of given array along with specified repetitions.
import numpy as np
arr = np.array([[2, 5, 8],[0, 4, 7]])
# Use numpy.tile() function to two-dimenstional array
repetitions = (3,2)
arr2 = np.tile(arr, repetitions)
print(arr2)
# OutPut:
# [[2 5 8 2 5 8]
# [0 4 7 0 4 7]
# [2 5 8 2 5 8]
# [0 4 7 0 4 7]
# [2 5 8 2 5 8]
# [0 4 7 0 4 7]]
# Use tile() function
arr2 = np.tile(arr, 2)
print(arr2)
# Output:
# [[2 5 8 2 5 8]
# [0 4 7 0 4 7]]
Frequently Asked Questions
The numpy.tile()
function is used to construct an array by repeating an input array along a specified number of times along each axis. It essentially tiles the input array to create a larger array with repeated patterns.
You can use numpy.tile()
with 1-D arrays. In fact, the numpy.tile()
function is quite versatile and can be applied to arrays of any dimension. When you use it with a 1-D array, it repeats the elements of the array along one or more axes.
To repeat an array vertically using numpy.tile()
, you need to use the reps
parameter appropriately. If you have a 1-D array and you want to repeat it vertically.
To repeat an array horizontally, you can use numpy.tile()
with the reps
parameter set to (1, num_repetitions)
or simply num_repetitions
to repeat along the second axis.
You can use numpy.tile()
to repeat an array along multiple axes. The reps
parameter in numpy.tile()
can take a tuple of integers, where each element specifies the number of repetitions along the corresponding axis.
You can pass a tuple to the reps
parameter, where each element of the tuple specifies the number of repetitions along the corresponding axis.
Conclusion
In this article, I have explained how to use numpy.tile()
function in Python and using this how to return tiled output arrays on 1-D and 2-D arrays.
Happy Learning!!
Related Articles
- Python NumPy Array Reshape
- How to Get NumPy Array Shape?
- How to append NumPy array ?
- NumPy concatenate() Function
- NumPy percentile() Function
- How to convert NumPy array to List
- Python NumPy nonzero() Function
- NumPy nanmean() – Get Mean ignoring NAN Values
- How to Use NumPy Random choice() in Python?
- How to Use NumPy random.normal() In Python?