|
| 1 | +--- |
| 2 | +Title: '.tile()' |
| 3 | +Description: 'Constructs a new array by repeating the input array’s elements a specified number of times.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Linear Algebra' |
| 9 | + - 'Machine Learning' |
| 10 | + - 'NumPy' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +In NumPy, the **`.tile()`** function constructs a new [array](https://www.codecademy.com/resources/docs/numpy/ndarray) by repeating the input array `A` according to the specified number of repetitions `reps`. |
| 17 | + |
| 18 | +- 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)`. |
| 19 | +- If `A.ndim > len(reps)`, the `reps` tuple is extended by prepending ones. For example, `reps=2` is treated as `(1, 2)`. |
| 20 | + |
| 21 | +## Syntax |
| 22 | + |
| 23 | +```pseudo |
| 24 | +numpy.tile(A, reps) |
| 25 | +``` |
| 26 | + |
| 27 | +**Parameters:** |
| 28 | + |
| 29 | +- `A`: The input array. |
| 30 | +- `reps`: The number of times the values need to be repeated. |
| 31 | + |
| 32 | +**Return value:** |
| 33 | + |
| 34 | +Returns a new array containing the input array's elements repeated a specific number of times. |
| 35 | + |
| 36 | +## Example: Repeating Elements in 1D, 2D, and 3D Arrays |
| 37 | + |
| 38 | +The following example shows how 1D, 2D, and 3D arrays interact with `.tile()`: |
| 39 | + |
| 40 | +```py |
| 41 | +import numpy as np |
| 42 | + |
| 43 | +# Create a 1D array |
| 44 | +a = np.array([3,2,1]) |
| 45 | +print("Shape of a:",np.shape(a)) |
| 46 | +print("a:",a) |
| 47 | + |
| 48 | +# Use .tile() to repeat the values twice horizontally |
| 49 | +b = np.tile(a,2) |
| 50 | +print("b:",b) |
| 51 | + |
| 52 | +# Use .tile() to create a 2D array |
| 53 | +c = np.tile(a,(1,2)) |
| 54 | +d = np.tile(a,(2,1)) |
| 55 | +print("c:",c) |
| 56 | +print("d:",d) |
| 57 | + |
| 58 | +# Use .tile() to create a 3D array |
| 59 | +e = np.tile(a,(2,1,2)) |
| 60 | +print("e:",e) |
| 61 | + |
| 62 | +print("\n\n") |
| 63 | + |
| 64 | +# Create a 2D array |
| 65 | +m = np.array([[5,7,8],[8,2,0]]) |
| 66 | +print("Shape of m:",np.shape(m)) |
| 67 | +print("m:",m) |
| 68 | + |
| 69 | +# Use .tile() to repeat the values twice horizontally |
| 70 | +n = np.tile(m,2) |
| 71 | +print("n:",n) |
| 72 | + |
| 73 | +# To repeat the values horizontally only |
| 74 | +o = np.tile(m,(1,2)) |
| 75 | +print("o:",o) |
| 76 | + |
| 77 | +# To repeat the values horizontally and vertically |
| 78 | +p = np.tile(m,(2,2)) |
| 79 | +print("p:",p) |
| 80 | + |
| 81 | +# To create a 3D array |
| 82 | +q = np.tile(m,(2,1,2)) |
| 83 | +print("q:",q) |
| 84 | +``` |
| 85 | + |
| 86 | +This produces the following output: |
| 87 | + |
| 88 | +```shell |
| 89 | +Shape of a: (3,) |
| 90 | +a: [3 2 1] |
| 91 | +b: [3 2 1 3 2 1] |
| 92 | +c: [[3 2 1 3 2 1]] |
| 93 | +d: [[3 2 1] |
| 94 | + [3 2 1]] |
| 95 | +e: [[[3 2 1 3 2 1]] |
| 96 | + |
| 97 | + [[3 2 1 3 2 1]]] |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +Shape of m: (2, 3) |
| 102 | +m: [[5 7 8] |
| 103 | + [8 2 0]] |
| 104 | +n: [[5 7 8 5 7 8] |
| 105 | + [8 2 0 8 2 0]] |
| 106 | +o: [[5 7 8 5 7 8] |
| 107 | + [8 2 0 8 2 0]] |
| 108 | +p: [[5 7 8 5 7 8] |
| 109 | + [8 2 0 8 2 0] |
| 110 | + [5 7 8 5 7 8] |
| 111 | + [8 2 0 8 2 0]] |
| 112 | +q: [[[5 7 8 5 7 8] |
| 113 | + [8 2 0 8 2 0]] |
| 114 | + |
| 115 | + [[5 7 8 5 7 8] |
| 116 | + [8 2 0 8 2 0]]] |
| 117 | +``` |
| 118 | + |
| 119 | +## Codebyte Example: Practical Usage of `.tile()` |
| 120 | + |
| 121 | +The following codebyte example shows the usage of the `.tile()` function: |
| 122 | + |
| 123 | +```codebyte/python |
| 124 | +import numpy as np |
| 125 | +
|
| 126 | +# Create a 1D and 2D array |
| 127 | +a = np.array([3,7,6]) |
| 128 | +b = np.array([[5,-2,8],[8,2,0]]) |
| 129 | +
|
| 130 | +print("Shape of a:",np.shape(a)) |
| 131 | +print("a:",a) |
| 132 | +print("Shape of b:",np.shape(b)) |
| 133 | +print("b:",b) |
| 134 | +
|
| 135 | +c = np.tile(a,(2,2)) |
| 136 | +d = np.tile(a,(4,1)) |
| 137 | +e = np.tile(b,1) |
| 138 | +f = np.tile(b,(3,1)) |
| 139 | +
|
| 140 | +print("\n") |
| 141 | +print("c:",c,"\t\n","d:",d,"\t\n") |
| 142 | +print("e:",e,"\t\n","f:",f,"\t\n") |
| 143 | +``` |
0 commit comments