Question
Based on the code pasted below, please: a) Plot the figures showing your predictions using degree = 2, 5, 8, 10, 20. Show the figures.
Based on the code pasted below, please:
a) Plot the figures showing your predictions using degree = 2, 5, 8, 10, 20. Show the figures.
b) Plot a figure tracking the change of training and testing loss using different models (degree = 2....25). Show the figure.
c) Explain which degree number(s) is/are the best and why?
CODE:
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
m = 500
np.random.seed(seed=5)
X = 6 * np.random.random(m).reshape(-1, 1) - 3
y = 0.5 * X**5 - X**3 - X**2 + 2 + 5 * np.random.randn(m, 1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.40, random_state=42)
reg1 = LinearRegression()
reg1.fit(X_train, y_train)
y_pred1 = reg1.predict(X_test)
degree = 10
poly = make_pipeline(PolynomialFeatures(degree), LinearRegression())
poly.fit(X,y)
y_pred2 = poly.predict(X_test)
score1 = mean_squared_error(y_test, y_pred1)
score2 = mean_squared_error(y_test, y_pred2)
print("Linear regression loss: ", score1)
print("Polynomial loss: ", score2)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started