About :
numpy.repeat(arr, repetitions, axis = None) : repeat elements of the array – arr.
Parameters :
array : [array_like]Input array.
repetitions : No. of repetitions of each array elements along the given axis.
axis : Axis along which we want to repeat values. By default, it returns
a flat output array.
Return :
An array with repetitions of array - arr elements as per repetitions, number of times we want to repeat arr
Code 1 :
# Python Program illustrating # numpy.repeat() import numpy as geek #Working on 1D arr = geek.arange(5) print("arr : \n", arr) repetitions = 2a = geek.repeat(arr, repetitions) print("\nRepeating arr 2 times : \n", a) print("Shape : ", a.shape) repetitions = 3a = geek.repeat(arr, repetitions) print("\nRepeating arr 3 times : \n", a) # [0 0 0 ..., 4 4 4] means [0 0 0 1 1 1 2 2 2 3 3 3 4 4 4] # since it was long output, so it uses [ ... ] print("Shape : ", a.shape) |
Output :
arr : [0 1 2 3 4] Repeating arr 2 times : [0 0 1 1 2 2 3 3 4 4] Shape : (10,) Repeating arr 3 times : [0 0 0 ..., 4 4 4] Shape : (15,)
Code 2 :
# Python Program illustrating # numpy.repeat() import numpy as geek arr = geek.arange(6).reshape(2, 3) print("arr : \n", arr) repetitions = 2print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 1)) print("arr Shape : \n", geek.repeat(arr, repetitions).shape) repetitions = 2print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 0)) print("arr Shape : \n", geek.repeat(arr, repetitions).shape) repetitions = 3print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 1)) print("arr Shape : \n", geek.repeat(arr, repetitions).shape) |
Output :
arr : [[0 1 2] [3 4 5]] Repeating arr : [[0 0 1 1 2 2] [3 3 4 4 5 5]] arr Shape : (12,) Repeating arr : [[0 1 2] [0 1 2] [3 4 5] [3 4 5]] arr Shape : (12,) Repeating arr : [[0 0 0 ..., 2 2 2] [3 3 3 ..., 5 5 5]] arr Shape : (18,)
References :
https://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.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 | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- Python | Visualizing O(n) using Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- MySQL-Connector-Python module in Python
- Python - Read blob object in python using wand library
- Python | PRAW - Python Reddit API Wrapper
- twitter-text-python (ttp) module - Python
- Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers
- Python program to check if the list contains three consecutive common numbers in Python
- Creating and updating PowerPoint Presentations in Python using python - pptx
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2

