Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The ground truth function for the regression is f(x) = 3 cos(1.3x) The following code generates 100 sample datasets with f(x) + Gaussian white noise

The ground truth function for the regression is f(x) = 3 cos(1.3x)

The following code generates 100 sample datasets with f(x) + Gaussian white noise (N(0,1)).

Each dataset will have 20 points randomly selected x from [0,5] with corresponding target points.

# Ground truth target function def f(x): return 3 * np.cos(1.3 * x)

# seed np.random.seed(62) # x x = np.random.uniform(0.0, 5.0, [100, 20]) x = np.sort(x)

# Ground truth targets g = f(x) # Add white noise noisy = np.random.normal(0, 1, [100, 20]) # y y = g + noisy

# use linspace(0,5,100) as test set to plot the images x_test = np.linspace(0,5,100)

Generate 4 figures using the first datasets. Use x_test to plot all the model functions, not just the ground truth function.

This will make all the higher polynomial models look smoother.

Figure one: Function f(x) = 3 cos(2x) and one noisy dataset sampled from the function, namely "Function, and data".

Figure two: Generate five polynomial fits of degree ONE based on the first five datasets and name this figure with "Order 1"

Figure three: Generate five polynomial fits of degree THREE based on the first five datasets and name this figure with "Order 3"

Figure four: Generate five polynomial fits of degree FIVE based on the first five datasets and name this figure with "Order 5"

For figures two, three, and four, please add a dotted line as an average line for the five fits.

Fill in the code below: from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.pipeline import Pipeline

def linear_model_predict(X, Y, order): # fit one polynomial model of degree `order` ## Insert your code BEGIN

## Insert your code END return model

def plot_figure(x, y, x_test, order): # plot five curves corresponding to the polynomial of degree `order` # plot the average of these five curves ## Insert your code BEGIN

## Insert your code END

# show the plots fig, axs = plt.subplots(2, 2, figsize=(15, 15))

# figure one plt.subplot(2, 2, 1) ## Insert your code BEGIN

## Insert your code END

# figure two plt.subplot(2, 2, 2) plt.ylim(-5, 5) plot_figure(x, y, x_test, order=1)

# figure three plt.subplot(2, 2, 3) plt.ylim(-5, 5) plot_figure(x, y, x_test, order=3)

# figure four plt.subplot(2, 2, 4) plt.ylim(-5, 5) plot_figure(x, y, x_test, order=5)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions