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.
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 Python NumPy tile() Function
If you are in a hurry, below are some quick examples of how to use NumPy tile() function in Python.
# Below are the quick examples
# 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 numpy.tile() function to vertically
repetitions = (2,1)
arr2 = np.tile(arr, repetitions)
# Example 4: Use numpy.tile() function to horizontally
repetitions = (3,2)
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 parameter represents the input array.reps:
The number of repetitions of arr 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). Let’s create a an array using numpy.array()
function. Subsequently, we used the numpy.tile()
function to multiply the array.
import numpy as np
arr = np.array([2, 5, 8])
# Use tile() function to 1-d array
arr2 = np.tile(arr, reps=2)
print(arr2)
# Output
# [2 5 8 2 5 8]
5. Use tile() Function To Vertically
To repeat the array multiple times vertically use the reps with tuple (2,1), this repeats the same NumPy array into vertically.
# Use numpy.tile() function to vertically
repetitions = (2,1)
arr2 = np.tile(arr, repetitions)
print(arr2)
# Output
# [[2 5 8]
# [2 5 8]]
6. Use tile() Function To Horizontally
We can also add the horizontal repetitions using np.tile()
function. Use tuple (3,3) which repeats elements both horizontally and vertically.
# Use numpy.tile() function to horizontally
repetitions = (3,2)
arr2 = np.tile(arr, repetitions)
print(arr2)
# OutPut
[[2 5 8 2 5 8]
[2 5 8 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]]
8. Conclusion
In this article, I have explained how to use numpy.tile()
function in Python and using this how to return tiled output array 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
- How to convert NumPy array to List
- NumPy nanmean() – Get Mean ignoring NAN Values
- How to Use NumPy Random choice() in Python?
- How to Use NumPy random.normal() In Python?