Question: The following code is used to generate 100 sample datasets with f(x) + Gaussian white noise (N(0,1)). Each dataset will have 20 points randomly selected
The following code is used to generate 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)
The following code is used to fit one polynomial model of degree `order`, and then plot the 5 polynomial fit curve corresponding to the polynomial of degree `order' using the first five datasets in x_test.
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))) plt.show
Now using all 100 datasets generated, what code will fit 100 models for each degree in range(1, 6) and stores it in list models_list[] (so the shape of models_list is (5, 100))? Then what code evaluate each of the 5 * 100 models on `x_eval` and stores it in models_evals_list (where x_eval = np.linspace(0, 5, 10))?
models_list=[]
models_evals_list=[]
x_eval = np.linspace(0, 5, 10)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
