|
| 1 | +import os |
| 2 | +import numpy as np |
| 3 | +import pandas as pd |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +from sklearn.model_selection import train_test_split |
| 6 | +from sklearn import linear_model |
| 7 | + |
| 8 | +def R2(y_data, y_model): |
| 9 | + return 1 - np.sum((y_data - y_model) ** 2) / np.sum((y_data - np.mean(y_data)) ** 2) |
| 10 | +def MSE(y_data,y_model): |
| 11 | + n = np.size(y_model) |
| 12 | + return np.sum((y_data-y_model)**2)/n |
| 13 | + |
| 14 | + |
| 15 | +# A seed just to ensure that the random numbers are the same for every run. |
| 16 | +# Useful for eventual debugging. |
| 17 | +np.random.seed(0) |
| 18 | + |
| 19 | +#x = np.random.rand(100) |
| 20 | +x = np.linspace(-1,1,200) |
| 21 | +y = 1.0/(1.0+25*x*x) |
| 22 | +plt.plot(x, y, label = 'Runge') |
| 23 | +# number of features p (here degree of polynomial |
| 24 | +p = 9 |
| 25 | +# The design matrix now as function of a given polynomial |
| 26 | +X = np.zeros((len(x),p)) |
| 27 | +X[:,0] = x |
| 28 | +X[:,1] = x*x |
| 29 | +X[:,2] = x*x*x |
| 30 | +X[:,3] = x*x*x*x |
| 31 | +X[:,4] = x*x*x*x*x |
| 32 | +X[:,5] = x*x*x*x*x*x |
| 33 | +X[:,6] = x**7 |
| 34 | +X[:,7] = x**8 |
| 35 | +X[:,8] = x**9 |
| 36 | +# We split the data in test and training data |
| 37 | +#X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) |
| 38 | + |
| 39 | +# matrix inversion to find beta |
| 40 | +#OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train |
| 41 | +OLSbeta = np.linalg.inv(X.T @ X) @ X.T @ y |
| 42 | +ypredict = X @ OLSbeta |
| 43 | +plt.plot(x, y, label = 'Runge') |
| 44 | +plt.plot(x, ypredict, label = 'Runge') |
| 45 | +plt.show() |
| 46 | +""" |
| 47 | +print(OLSbeta) |
| 48 | +# and then make the prediction |
| 49 | +ytildeOLS = X_train @ OLSbeta |
| 50 | +print("Training MSE for OLS") |
| 51 | +print(MSE(y_train,ytildeOLS)) |
| 52 | +ypredictOLS = X_test @ OLSbeta |
| 53 | +print("Test MSE OLS") |
| 54 | +print(np.abs(y_test-ypredictOLS)) |
| 55 | +print(MSE(y_test,ypredictOLS)) |
| 56 | +
|
| 57 | +# Repeat now for Lasso and Ridge regression and various values of the regularization parameter |
| 58 | +I = np.eye(p,p) |
| 59 | +# Decide which values of lambda to use |
| 60 | +nlambdas = 100 |
| 61 | +MSEPredict = np.zeros(nlambdas) |
| 62 | +MSETrain = np.zeros(nlambdas) |
| 63 | +MSELassoPredict = np.zeros(nlambdas) |
| 64 | +MSELassoTrain = np.zeros(nlambdas) |
| 65 | +lambdas = np.logspace(-4, 4, nlambdas) |
| 66 | +for i in range(nlambdas): |
| 67 | + lmb = lambdas[i] |
| 68 | + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train |
| 69 | + # include lasso using Scikit-Learn |
| 70 | + RegLasso = linear_model.Lasso(lmb,fit_intercept=True) |
| 71 | + RegLasso.fit(X_train,y_train) |
| 72 | + # and then make the prediction |
| 73 | + ytildeRidge = X_train @ Ridgebeta |
| 74 | + ypredictRidge = X_test @ Ridgebeta |
| 75 | + ytildeLasso = RegLasso.predict(X_train) |
| 76 | + ypredictLasso = RegLasso.predict(X_test) |
| 77 | + MSEPredict[i] = MSE(y_test,ypredictRidge) |
| 78 | + MSETrain[i] = MSE(y_train,ytildeRidge) |
| 79 | + MSELassoPredict[i] = MSE(y_test,ypredictLasso) |
| 80 | + MSELassoTrain[i] = MSE(y_train,ytildeLasso) |
| 81 | +
|
| 82 | +# Now plot the results |
| 83 | +
|
| 84 | +plt.figure() |
| 85 | +plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train') |
| 86 | +plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test') |
| 87 | +plt.plot(np.log10(lambdas), MSELassoTrain, label = 'MSE Lasso train') |
| 88 | +plt.plot(np.log10(lambdas), MSELassoPredict, 'r--', label = 'MSE Lasso Test') |
| 89 | +
|
| 90 | +plt.xlabel('log10(lambda)') |
| 91 | +plt.ylabel('MSE') |
| 92 | +plt.legend() |
| 93 | +plt.show() |
| 94 | +""" |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
0 commit comments