Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions