how to smooth a function in python

Solutions on MaxInterview for how to smooth a function in python by the best coders in the world

showing results for - "how to smooth a function in python"
Facundo
01 Mar 2020
1x = np.linspace(0,2*np.pi,100)
2y = np.sin(x) + np.random.random(100) * 0.8
3
4def smooth(y, box_pts):
5    box = np.ones(box_pts)/box_pts
6    y_smooth = np.convolve(y, box, mode='same')
7    return y_smooth
8
9plot(x, y,'o')
10plot(x, smooth(y,3), 'r-', lw=2)
11plot(x, smooth(y,19), 'g-', lw=2)