I have a list :
a = [1,2,3,4,5,6,7,8,9,10]
I want to add a different random number to each element in the list using np.random.randn() I start by creating another list containing the rando values by using:
noise = []
for i in range(len(a)):
b = (random_noise * np.random.randn())
noise.append(b)
And the add the two lists
a b
My question is, is there a simplified method in which i can add the noise directly to a elements without the need to creating the loop, in seek of saving time in the large problems.
CodePudding user response:
If you turn a
into a numpy array, you can do
>>> a = np.array(a)
>>> a2 = a np.random.randn(a.size)
>>> a2
array([-0.1199694 , 1.64558727, 3.10977101, 5.66627737, 4.95481395,
7.63834891, 7.48148948, 8.55867759, 8.02858298, 9.77297563])
CodePudding user response:
Create the noise as np array and add it to a
import numpy as np
a = [1,2,3,4,5,6,7,8,9,10]
noise = np.random.randn(len(a))
print(a noise)
CodePudding user response:
You can do something like this:
a = [x np.random.randn() for x in range(1, 10)]
print(a)
output:
[0.31328085156307206, 0.8473128208005949, 4.574220370196114, 3.7338991251803195, 4.9131070198281686, 5.4760389038550565, 7.328982658187497, 6.716468468134682, 9.491303138695487]