mercredi 6 janvier 2021

How to add a random number to each value of an array [Python]

I was wondering how to add a random number in a given range to each value of an array. I have an array composed by 1000 values which, when plotted, give me an S-curve. The thing is, I want to add noise to this curve, which is why I want to add a random number to the values of the array, that will change for each value.

My current code is this :

import numpy as np
import matplotlib.pyplot as plt
import random

def sigmoid(z):
    return 1/(1+np.exp(-z))

n=random.randint(-0.1,0.1)
y = np.arange(-5, 5, 0.01)
print(y)

for i in range(0,1000):
    y[i]=y[i]+n
    i=i+1
    
plt.plot(y, sigmoid(y), marker='x')
plt.show()

But this error comes up : "non-integer arg 1 for randrange()" Do you have any idea on how to solve this problem?

Thank you very much!




Aucun commentaire:

Enregistrer un commentaire