From 3343609fdec4e82090da031fff4eaa6aba5ea9f3 Mon Sep 17 00:00:00 2001 From: Morten Hjorth-Jensen Date: Sun, 20 Aug 2023 21:10:47 +0200 Subject: [PATCH] Update week34.do.txt --- doc/src/week34/week34.do.txt | 83 +++++++++++++++++------------------- 1 file changed, 40 insertions(+), 43 deletions(-) diff --git a/doc/src/week34/week34.do.txt b/doc/src/week34/week34.do.txt index 338f15bca..78471bab3 100644 --- a/doc/src/week34/week34.do.txt +++ b/doc/src/week34/week34.do.txt @@ -2563,50 +2563,47 @@ where we have defined the mean value of $\bm{y}$ as You can use the functionality included in scikit-learn. If you feel for it, you can use your own program and define functions which compute the above two functions. Discuss the meaning of these results. Try also to vary the coefficient in front of the added stochastic noise term and discuss the quality of the fits. -!bsol -The code here is an example of where we define our own design matrix and fit parameters $\beta$. +===== Exercise: Split data in test and training data ===== + +In this exercise we want you to to compute the MSE for the training +data and the test data as function of the complexity of a polynomial, +that is the degree of a given polynomial. + +One of +the aims is to reproduce Figure 2.11 of "Hastie et al":"https://github.com/CompPhysics/MLErasmus/blob/master/doc/Textbooks/elementsstat.pdf". + +Our data is defined by $x\in [-3,3]$ with a total of for example $100$ data points. !bc pycod -import os -import numpy as np -import pandas as pd -import matplotlib.pyplot as plt -from sklearn.model_selection import train_test_split - -def save_fig(fig_id): - plt.savefig(image_path(fig_id) + ".png", format='png') - -def R2(y_data, y_model): - return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2) -def MSE(y_data,y_model): - n = np.size(y_model) - return np.sum((y_data-y_model)**2)/n - -x = np.random.rand(100) -y = 2.0+5*x*x+0.1*np.random.randn(100) - - -# The design matrix now as function of a given polynomial -X = np.zeros((len(x),3)) -X[:,0] = 1.0 -X[:,1] = x -X[:,2] = x**2 -# We split the data in test and training data -X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) -# matrix inversion to find beta -beta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(beta) -# and then make the prediction -ytilde = X_train @ beta -print("Training R2") -print(R2(y_train,ytilde)) -print("Training MSE") -print(MSE(y_train,ytilde)) -ypredict = X_test @ beta -print("Test R2") -print(R2(y_test,ypredict)) -print("Test MSE") -print(MSE(y_test,ypredict)) +np.random.seed() +n = 100 +maxdegree = 14 +# Make data set. +x = np.linspace(-3, 3, n).reshape(-1, 1) +y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)+ np.random.normal(0, 0.1, x.shape) !ec -!esol +where $y$ is the function we want to fit with a given polynomial. +!bsubex +Write a first code which sets up a design matrix $X$ defined by a fifth-order polynomial and split your data set in training and test data. +!esubex + +!bsubex +Perform an ordinary least squares and compute the means squared error for the training data and the test data. +!esubex + +!bsubex +Add now a model which allows you to make polynomials up to degree $15$. Perform a standard OLS fitting of the training data and compute the MSE for the training and test data and plot both test and training data MSE as functions of the polynomial degree. Compare what you see with Figure 2.11 of Hastie et al. Comment your results. For which polynomial degree do you find an optimal MSE (smallest value)? +!esubex + + + + + + + + + + + +