numpy.tile() in Python
About :
numpy.tile(arr, repetitions) : constructs a new array by repeating array – ‘arr’, the number of times we want to repeat as per repetitions. The resulted array will have dimensions max(arr.ndim, repetitions) where, repetitions is the length of repetitions.
If arr.ndim > repetitions, reps is promoted to arr.ndim by pre-pending 1’s to it.
If arr.ndim < repetitions, reps is promoted to arr.ndim by pre-pending new axis.
Parameters :
array : [array_like]Input array. repetitions : No. of repetitions of arr along each axis.
Return :
An array with repetitions of array - arr as per d, number of times we want to repeat arr
Code 1 :
# Python Program illustrating # numpy.tile() import numpy as geek #Working on 1D arr = geek.arange(5) print("arr : \n", arr) repetitions = 2print("Repeating arr 2 times : \n", geek.tile(arr, repetitions)) repetitions = 3print("\nRepeating arr 3 times : \n", geek.tile(arr, repetitions)) # [0 1 2 ..., 2 3 4] means [0 1 2 3 4 0 1 2 3 4 0 1 2 3 4] # since it was long output, so it uses [ ... ] |
Output :
arr : [0 1 2 3 4] Repeating arr 2 times : [0 1 2 3 4 0 1 2 3 4] Repeating arr 3 times : [0 1 2 ..., 2 3 4]
Code 2 :
# Python Program illustrating # numpy.tile() import numpy as geek arr = geek.arange(3) print("arr : \n", arr) a = 2 b = 2 repetitions = (a, b) print("\nRepeating arr : \n", geek.tile(arr, repetitions)) print("arr Shape : \n", geek.tile(arr, repetitions).shape) a = 3 b = 2 repetitions = (a, b) print("\nRepeating arr : \n", geek.tile(arr, repetitions)) print("arr Shape : \n", geek.tile(arr, repetitions).shape) a = 2b = 3 repetitions = (a, b) print("\nRepeating arr : \n", geek.tile(arr, repetitions)) print("arr Shape : \n", geek.tile(arr, repetitions).shape) |
Output :
arr : [0 1 2] Repeating arr : [[0 1 2 0 1 2] [0 1 2 0 1 2]] arr Shape : (2, 6) Repeating arr : [[0 1 2 0 1 2] [0 1 2 0 1 2] [0 1 2 0 1 2]] arr Shape : (3, 6) Repeating arr : [[0 1 2 ..., 0 1 2] [0 1 2 ..., 0 1 2]] arr Shape : (2, 9)
Code 3 : (repetitions == arr.ndim) == 0
# Python Program illustrating # numpy.tile() import numpy as geek arr = geek.arange(4).reshape(2, 2) print("arr : \n", arr) a = 2 b = 1 repetitions = (a, b) print("\nRepeating arr : \n", geek.tile(arr, repetitions)) print("arr Shape : \n", geek.tile(arr, repetitions).shape) a = 3 b = 2 repetitions = (a, b) print("\nRepeating arr : \n", geek.tile(arr, repetitions)) print("arr Shape : \n", geek.tile(arr, repetitions).shape) a = 2b = 3 repetitions = (a, b) print("\nRepeating arr : \n", geek.tile(arr, repetitions)) print("arr Shape : \n", geek.tile(arr, repetitions).shape) |
Output :
arr : [[0 1] [2 3]] Repeating arr : [[0 1] [2 3] [0 1] [2 3]] arr Shape : (4, 2) Repeating arr : [[0 1 0 1] [2 3 2 3] [0 1 0 1] [2 3 2 3] [0 1 0 1] [2 3 2 3]] arr Shape : (6, 4) Repeating arr : [[0 1 0 1 0 1] [2 3 2 3 2 3] [0 1 0 1 0 1] [2 3 2 3 2 3]] arr Shape : (4, 6)
References :
https://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html
Note :
These codes won’t run on online-ID. Please run them on your systems to explore the working
.
This article is contributed by Mohit Gupta_OMG 😀. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- Python | Convert list to Python array
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- SHA in Python
- Any & All in Python
- bin() in Python
- Use of min() and max() in Python



