Question
Under the plot_figures function, what code plots the average of the five curves (that correspond to the polynomial of degree order)? import random import numpy
Under the plot_figures function, what code plots the average of the five curves (that correspond to the polynomial of degree "order")?
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LinearRegression from sklearn.pipeline import Pipeline
# 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)
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` model = Pipeline([('poly', PolynomialFeatures(degree=order)), ('linear', LinearRegression(fit_intercept=False))]) model.fit(X.reshape(-1,1), Y) 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 plt.title("Order "+str(order)) #plot five curves for n in range(5): model=linear_model_predict(x[n], y[n], order) plt.plot(x_test, model.predict(x_test.reshape(-1,1))) #plot average of curves plt.legend() plt.show # show the plots fig, axs = plt.subplots(2, 2, figsize=(15, 15)) # figure one plt.subplot(2, 2, 1) plt.title("Function and data") plt.plot(x_test, f(x_test)) plt.plot(x[0], y[0], 'o') # 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
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