Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added file on tile #6476

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions content/numpy/concepts/built-in-functions/terms/tile/tile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
---
Title: '.tile()'
Description: "Constructs a new array by repeating the input array’s elements a specified number of times."
Subjects:
- 'Computer Science'
- 'Data Science'
- 'Machine Learning'
Tags:
- 'Linear Algebra'
- 'Machine Learning'
- 'NumPy'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

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 of `A` are promoted by prepending ones to match the length of `reps`. For example, an array with shape `(3,)` will be treated as `(1, 3)`.
- If `A.ndim > len(reps)`, the `reps` tuple is extended by prepending ones. For example, `reps=2` is treated as `(1, 2)`.

## Syntax

```pseudo
numpy.tile(A, reps)
```

**Parameters:**

- `A`: The input array.
- `reps`: The number of times the values need to be repeated.

## Example: Repeating Elements in 1D, 2D, and 3D Arrays

The following example show how 1D and 2D arrays interact with `.tile()`:

```py
import numpy as np

# Create a 1D array
a = np.array([3,2,1])
print("Shape of a:",np.shape(a))
print("a:",a)

# Use numpy.tile() to repeat the values twice horizontally
b = np.tile(a,2)
print("b:",b)

# Use numpy.tile() to create a 2D array
c = np.tile(a,(1,2))
d = np.tile(a,(2,1))
print("c:",c)
print("d:",d)

# Use numpy.tile() to create a 3D array
e = np.tile(a,(2,1,2))
print("e:",e)

print("\n\n")

# Create a 2D array
m = np.array([[5,7,8],[8,2,0]])
print("Shape of m:",np.shape(m))
print("m:",m)

# Use numpy.tile() to repeat the values twice horizontally
n = np.tile(m,2)
print("n:",n)

# To repeat values horizontally only
o = np.tile(m,(1,2))
print("o:",o)

# To repeat values horizontally and vertically
p = np.tile(m,(2,2))
print("p:",p)

# To create a 3D array
q = np.tile(m,(2,1,2))
print("q:",q)
```

This produces the following output:

```shell
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:

```codebyte/python
import numpy as np

#Create a 1D and 2D array
a = np.array([3,7,6])
b = np.array([[5,-2,8],[8,2,0]])

print("Shape of a:",np.shape(a))
print("a:",a)
print("Shape of b:",np.shape(b))
print("b:",b)

c = np.tile(a,(2,2))
d = np.tile(a,(4,1))
e = np.tile(b,1)
f = np.tile(b,(3,1))

print("\n")
print("c:",c,"\t\n","d:",d,"\t\n")
print("e:",e,"\t\n","f:",f,"\t\n")
```
Loading