Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python NumPy repeat Function Syntax

numpy.repeat(arr, repetitions, axis = None)
Comment

Python NumPy repeat Function Example

# welcome to softhunt.net
# Python Program illustrating
# numpy.repeat()

import numpy as np

arr = np.arange(6).reshape(2, 3)
print("arr : 
", arr)

repetitions = 2
print("
Repeating arr : 
", np.repeat(arr, repetitions, 1))
print("arr Shape : 
", np.repeat(arr, repetitions).shape)


repetitions = 2
print("
Repeating arr : 
", np.repeat(arr, repetitions, 0))
print("arr Shape : 
", np.repeat(arr, repetitions).shape)
	
repetitions = 3
print("
Repeating arr : 
", np.repeat(arr, repetitions, 1))
print("arr Shape : 
", np.repeat(arr, repetitions).shape)
Comment

repeat a numpy array

a = np.array([0, 1, 2])
>>> np.tile(a, 2)
array([0, 1, 2, 0, 1, 2])
>>> np.tile(a, (2, 2))
array([[0, 1, 2, 0, 1, 2],
       [0, 1, 2, 0, 1, 2]])
>>> np.tile(a, (2, 1, 2))
array([[[0, 1, 2, 0, 1, 2]],
       [[0, 1, 2, 0, 1, 2]]])
Comment

Python NumPy repeat Function Example Working with 1D array

# welcome to softhunt.net
# Python Program illustrating
# numpy.repeat()

import numpy as np

#Working on 1D
arr = np.arange(5)
print("arr : 
", arr)

repetitions = 2
a = np.repeat(arr, repetitions)
print("
Repeating arr 2 times : 
", a)
print("Shape : ", a.shape)

repetitions = 3
a = np.repeat(arr, repetitions)
print("
Repeating arr 3 times : 
", 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)
Comment

PREVIOUS NEXT
Code Example
Python :: python if and 
Python :: pickle load data 
Python :: represent NaN with pandas in python 
Python :: can only concatenate str (not "int") to str 
Python :: how to print horizontally in python 
Python :: Range python iterate by 2 
Python :: turtle keep window open 
Python :: python rps 
Python :: pandas cartesian product 
Python :: how to terminate subprocess.call in python 
Python :: reading doc in python 
Python :: python convert image to base64 
Python :: find value in dictionary python 
Python :: generate rsa key python 
Python :: insert single value in dataframe using index 
Python :: driver code in python 
Python :: torch.stack example 
Python :: django pagination 
Python :: django insert template in another template 
Python :: save model history keras 
Python :: integral python 
Python :: get body from request python 
Python :: python match statement 
Python :: python 3.7.9 download 
Python :: python count how many times a character appears in a string 
Python :: python enum advanced 
Python :: how to make a leaderboard in python 
Python :: Python NumPy broadcast_arrays() Function Example 
Python :: range(len()) in python 
Python :: split string into groups of 3 chars python 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =