Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- data=np.genfromtxt('artificial.txt', delimiter=',', skip_header=1) # Columns: x, y
- def generate_poly_features(x, q):
- # Generate "design matrix" for the polynomial regression model
- # y= θ_0 + θ_1*x + θ_1*x^2 + ... + θ_q*x^q
- # The matrix has(q + 1) columns where column i contains elements
- # of the vector x raised to the i-th power
- X=np.ones(shape=(x.shape[0],q+1))
- for i in range(0,q+1):
- X[:,i] = np.power(x,i)
- return X
- def poly_fit(x, y, q):
- # Pre: x and y are 1-dimensional arrays
- theta = np.linalg.solve(X.T.dot(x), X.T.dot(y))
- return theta
- def poly_predict(xtest, theta):
- ypred=xtest.dot(theta)
- return ypred
- # Load data, predict and plot
- x=data[:,0]
- y=data[:,1]
- deg = 3
- X=generate_poly_features(x, deg)
- theta = poly_fit(X,y,deg)
- ypred = poly_predict(X, theta)
- plt.plot(x,ypred, color="red")
- plt.scatter(x,y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement