.tile()
Anonymous contributor
Published Apr 11, 2025
Contribute to Docs
In NumPy, the .tile()
function constructs a new array by repeating the input array A
according to the specified number of repetitions reps
.
- If
A.ndim < len(reps)
, dimensions ofA
are promoted by prepending ones to match the length ofreps
. For example, an array with shape(3,)
will be treated as(1, 3)
. - If
A.ndim > len(reps)
, thereps
tuple is extended by prepending ones. For example,reps=2
is treated as(1, 2)
.
Syntax
numpy.tile(A, reps)
Parameters:
A
: The input array.reps
: The number of times the values need to be repeated.
Return value:
Returns a new array containing the input array’s elements repeated a specific number of times.
Example: Repeating Elements in 1D, 2D, and 3D Arrays
The following example shows how 1D, 2D, and 3D arrays interact with .tile()
:
import numpy as np# Create a 1D arraya = np.array([3,2,1])print("Shape of a:",np.shape(a))print("a:",a)# Use .tile() to repeat the values twice horizontallyb = np.tile(a,2)print("b:",b)# Use .tile() to create a 2D arrayc = np.tile(a,(1,2))d = np.tile(a,(2,1))print("c:",c)print("d:",d)# Use .tile() to create a 3D arraye = np.tile(a,(2,1,2))print("e:",e)print("\n\n")# Create a 2D arraym = np.array([[5,7,8],[8,2,0]])print("Shape of m:",np.shape(m))print("m:",m)# Use .tile() to repeat the values twice horizontallyn = np.tile(m,2)print("n:",n)# To repeat the values horizontally onlyo = np.tile(m,(1,2))print("o:",o)# To repeat the values horizontally and verticallyp = np.tile(m,(2,2))print("p:",p)# To create a 3D arrayq = np.tile(m,(2,1,2))print("q:",q)
This produces the following output:
Shape of a: (3,)a: [3 2 1]b: [3 2 1 3 2 1]c: [[3 2 1 3 2 1]]d: [[3 2 1][3 2 1]]e: [[[3 2 1 3 2 1]][[3 2 1 3 2 1]]]Shape of m: (2, 3)m: [[5 7 8][8 2 0]]n: [[5 7 8 5 7 8][8 2 0 8 2 0]]o: [[5 7 8 5 7 8][8 2 0 8 2 0]]p: [[5 7 8 5 7 8][8 2 0 8 2 0][5 7 8 5 7 8][8 2 0 8 2 0]]q: [[[5 7 8 5 7 8][8 2 0 8 2 0]][[5 7 8 5 7 8][8 2 0 8 2 0]]]
Codebyte Example: Practical Usage of .tile()
The following codebyte example shows the usage of the .tile()
function:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours