|
| 1 | +--- |
| 2 | +Title: '.repeat()' |
| 3 | +Description: 'Duplicates elements in an array along a given axis.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Data Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Data Structures' |
| 10 | + - 'Functions' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/data-science' |
| 15 | +--- |
| 16 | + |
| 17 | +The **`.repeat()`** function in NumPy is used to duplicate items in an array. It provides the option to specify the number of times each element appears, and whether that repetition happens across a specific axis or not. If no axis is mentioned, the array is flattened before repeating. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +numpy.repeat(a, repeats, axis=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `a`: The array to work with. This is where the elements come from. |
| 28 | +- `repeats`: An `int` or array of `ints`. If an `int`, each element of `a` is repeated that many times. If an array, it must match the length of `a` (if `axis=None`) or the length of `a` along the specified axis. |
| 29 | +- `axis` (Optional): Sets the direction for repeating. |
| 30 | + |
| 31 | +**Return value:** |
| 32 | + |
| 33 | +Returns a NumPy array with repeated elements. The shape of the result depends on the shape of `a`, the `repeats` value, and whether an `axis` is specified. |
| 34 | + |
| 35 | +## Example 1: Repeating Elements Without Specifying the `axis` Parameter |
| 36 | + |
| 37 | +Here's a simple example where each value in a one-dimensional array gets repeated twice: |
| 38 | + |
| 39 | +```py |
| 40 | +import numpy as np |
| 41 | + |
| 42 | +# Create an array |
| 43 | +arr = np.array([1, 2, 3]) |
| 44 | + |
| 45 | +# Repeat each element in the array 2 times |
| 46 | +print(np.repeat(arr, 2)) |
| 47 | +``` |
| 48 | + |
| 49 | +The above code produces the following output: |
| 50 | + |
| 51 | +```shell |
| 52 | +[1 1 2 2 3 3] |
| 53 | +``` |
| 54 | + |
| 55 | +## Example 2: Directional Repetition Using the `axis` Parameter |
| 56 | + |
| 57 | +For multi-dimensional arrays, using the `axis` parameter controls which direction the repetition flows: |
| 58 | + |
| 59 | +```py |
| 60 | +import numpy as np |
| 61 | + |
| 62 | +arr2d = np.array([[1, 2], [3, 4]]) |
| 63 | + |
| 64 | +# Repeat each element in the array 2 times along axis 0 |
| 65 | +print(np.repeat(arr2d, 2, axis=0)) |
| 66 | + |
| 67 | +# Repeat each element in the array 2 times along axis 1 |
| 68 | +print(np.repeat(arr2d, 2, axis=1)) |
| 69 | +``` |
| 70 | + |
| 71 | +The above code produces the following output: |
| 72 | + |
| 73 | +```shell |
| 74 | +[[1 2] |
| 75 | + [1 2] |
| 76 | + [3 4] |
| 77 | + [3 4]] |
| 78 | +[[1 1 2 2] |
| 79 | + [3 3 4 4]] |
| 80 | +``` |
| 81 | + |
| 82 | +## Codebyte Example: Repeating Each Element Three Times |
| 83 | + |
| 84 | +This codebyte example repeats every item in an array three times in a row: |
| 85 | + |
| 86 | +```codebyte/python |
| 87 | +import numpy as np |
| 88 | +
|
| 89 | +# Create an array |
| 90 | +arr = np.array([4, 5, 6]) |
| 91 | +
|
| 92 | +# Repeat each element in the array 3 times |
| 93 | +output = np.repeat(arr, 3) |
| 94 | +
|
| 95 | +print(output) |
| 96 | +``` |
0 commit comments