Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from IPython.display import clear_output
- f_true = lambda x: 2*x*np.sin(5*x) + x**2 - 1 # this is the true function
- # We need this to make the plot of f_true:
- x_grid = np.linspace(-2,5,100) # 100 linearly spaced numbers
- x_grid_enl = np.hstack((x_grid.reshape((100,1))**j for j in range(6)))
- y_grid = f_true(x_grid)
- A = np.dot(x_grid_enl.T, x_grid_enl)
- B = np.dot((x_grid_enl).T, y_grid)
- for i in range(200):
- x_new = np.random.uniform(-2, 5)
- y_new = f_true(x_new) + 2*np.random.randn()
- p = [x_new**j for j in range(6)]
- for l in range(len(A)):
- for j in range(len(A)):
- A[j,l] += p[l] * p[j]
- for l in range(len(B)):
- B[l] += p[l] * y_new
- koeff = np.dot(np.linalg.inv(A), B)
- # the rest of code is just bells and whistles
- if (i+1)%5==0:
- clear_output(True)
- plt.plot(x_grid,y_grid, color='blue', label='true f')
- plt.scatter(x_new, y_new, color='red')
- y_pred = [koeff[0] + koeff[1] * (i) + koeff[2] * (i**2) + koeff[3] * (i**3) +
- koeff[4] * (i**4) + koeff[5] * (i**5) for i in x_grid]
- plt.scatter(x_grid, y_pred, color='orange', linewidth=5, label='predicted f')
- plt.legend(loc='upper left')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement