Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from matplotlib import pyplot as plt
- import numpy as np
- def f(x):
- return x**2
- """calculate normal at x"""
- def get_normal(f, x):
- dx = 0.05
- dy = f(x+dx)-f(x)
- length = (dx**2+dy**2)**0.5
- return dy/length,-dx/length
- """generate size # of points in range lower to upper"""
- def generate_points(f, lower, upper, size):
- xs = lower + (upper-lower) * np.random.sample(size)
- return xs, [f(x) for x in xs]
- """
- apply noise on the x,y points (both are lists).
- size denotes the intensity of noise, should be trialed
- f is the function, needed for calculating the normal
- """
- def apply_noise(f, x, y, size):
- xs = []
- ys = []
- for point in zip(x,y):
- dx, dy = get_normal(f, point[0])
- xs.append(point[0]+dx*size*np.random.randn(1))
- ys.append(point[1]+dy*size*np.random.randn(1))
- return xs, ys
- if __name__=='__main__':
- x, y = generate_points(f, 0, 1, 100) # generate points
- plt.scatter(x, y, color='b') # plot in blue
- x, y = apply_noise(f, x, y, 0.08) # apply noises
- plt.scatter(x, y, color='r') # plot in red
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement