From 40a03f15f4c55a6ee42a18f9a3da38e4118a3d1f Mon Sep 17 00:00:00 2001 From: Neema Date: Fri, 4 Apr 2025 12:44:40 +0530 Subject: [PATCH 1/3] Added file on tile --- .../built-in-functions/terms/tile/tile.md | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 content/numpy/concepts/built-in-functions/terms/tile/tile.md diff --git a/content/numpy/concepts/built-in-functions/terms/tile/tile.md b/content/numpy/concepts/built-in-functions/terms/tile/tile.md new file mode 100644 index 00000000000..1dadb6cac60 --- /dev/null +++ b/content/numpy/concepts/built-in-functions/terms/tile/tile.md @@ -0,0 +1,142 @@ +--- +Title: '.tile()' +Description: 'Constructs an array by repeating the elements of another array' +Subjects: + - 'Computer Science' + - 'Data Science' + - 'Machine Learning' +Tags: + - 'Linear Algebra' + - 'Machine Learning' + - 'NumPy' +CatalogContent: + - 'learn-python-3' + - 'paths/computer-science' + - 'paths/data-science' + - 'paths/data-science-foundations' +--- + +The **`.tile()`** function is used to build an array by repeating the elements of another defined array A a fixed number of times (reps). The shape of the new array is determined by max(len(reps), A.ndim). + +- If A.ndim < len(reps) then dimensions of A will be changed. So, shape(3,) of an array will become (1,3) +- If A.ndim > len(reps) then dimensions of reps will be changed. So, reps(2) will prepend 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 + +The following example show how a 1D and 2D arrays interact with numpy.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 + +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") +``` From 1dcf2f4ae63fb8f3fff8390efd3c887aa722cfe5 Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 8 Apr 2025 14:50:32 +0530 Subject: [PATCH 2/3] minor --- .../built-in-functions/terms/tile/tile.md | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/content/numpy/concepts/built-in-functions/terms/tile/tile.md b/content/numpy/concepts/built-in-functions/terms/tile/tile.md index 1dadb6cac60..c360045a46c 100644 --- a/content/numpy/concepts/built-in-functions/terms/tile/tile.md +++ b/content/numpy/concepts/built-in-functions/terms/tile/tile.md @@ -1,6 +1,6 @@ --- Title: '.tile()' -Description: 'Constructs an array by repeating the elements of another array' +Description: "Constructs a new array by repeating the input array’s elements a specified number of times." Subjects: - 'Computer Science' - 'Data Science' @@ -12,14 +12,12 @@ Tags: CatalogContent: - 'learn-python-3' - 'paths/computer-science' - - 'paths/data-science' - - 'paths/data-science-foundations' --- -The **`.tile()`** function is used to build an array by repeating the elements of another defined array A a fixed number of times (reps). The shape of the new array is determined by max(len(reps), A.ndim). +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) then dimensions of A will be changed. So, shape(3,) of an array will become (1,3) -- If A.ndim > len(reps) then dimensions of reps will be changed. So, reps(2) will prepend as (1,2) +- 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 @@ -27,14 +25,14 @@ The **`.tile()`** function is used to build an array by repeating the elements o numpy.tile(A, reps) ``` -Parameters: +**Parameters:** -- `A`: The input array -- `reps`: The number of times the values need to be repeated +- `A`: The input array. +- `reps`: The number of times the values need to be repeated. -## Example +## Example: Repeating Elements in 1D, 2D, and 3D Arrays -The following example show how a 1D and 2D arrays interact with numpy.tile(): +The following example show how 1D and 2D arrays interact with `.tile()`: ```py import numpy as np @@ -115,11 +113,11 @@ q: [[[5 7 8 5 7 8] [8 2 0 8 2 0]]] ``` -## Codebyte Example +## Codebyte Example: Practical Usage of `.tile()` The following codebyte example shows the usage of the `.tile()` function: -```codebyte\python +```codebyte/python import numpy as np #Create a 1D and 2D array From 5be787ef4fb03d9fbec6e68ab6d6f50c9c5006a9 Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Fri, 11 Apr 2025 09:25:22 +0530 Subject: [PATCH 3/3] Minor changes --- .../built-in-functions/terms/tile/tile.md | 25 +++++++++++-------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/content/numpy/concepts/built-in-functions/terms/tile/tile.md b/content/numpy/concepts/built-in-functions/terms/tile/tile.md index c360045a46c..26edfbef4de 100644 --- a/content/numpy/concepts/built-in-functions/terms/tile/tile.md +++ b/content/numpy/concepts/built-in-functions/terms/tile/tile.md @@ -1,10 +1,9 @@ --- Title: '.tile()' -Description: "Constructs a new array by repeating the input array’s elements a specified number of times." +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' @@ -14,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -The **`.tile()`** function constructs a new array by repeating the input array `A` according to the specified number of repetitions `reps`. +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`. - 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)`. @@ -30,9 +29,13 @@ numpy.tile(A, reps) - `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 show how 1D and 2D arrays interact with `.tile()`: +The following example shows how 1D, 2D, and 3D arrays interact with `.tile()`: ```py import numpy as np @@ -42,17 +45,17 @@ 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 +# Use .tile() to repeat the values twice horizontally b = np.tile(a,2) print("b:",b) -# Use numpy.tile() to create a 2D array +# Use .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 +# Use .tile() to create a 3D array e = np.tile(a,(2,1,2)) print("e:",e) @@ -63,15 +66,15 @@ 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 +# Use .tile() to repeat the values twice horizontally n = np.tile(m,2) print("n:",n) -# To repeat values horizontally only +# To repeat the values horizontally only o = np.tile(m,(1,2)) print("o:",o) -# To repeat values horizontally and vertically +# To repeat the values horizontally and vertically p = np.tile(m,(2,2)) print("p:",p) @@ -120,7 +123,7 @@ The following codebyte example shows the usage of the `.tile()` function: ```codebyte/python import numpy as np -#Create a 1D and 2D array +# Create a 1D and 2D array a = np.array([3,7,6]) b = np.array([[5,-2,8],[8,2,0]])