diff --git a/doc/Projects/2020/hw2/html/._hw2-bs000.html b/doc/Projects/2020/hw2/html/._hw2-bs000.html index 847089b0c..a6e6c8963 100644 --- a/doc/Projects/2020/hw2/html/._hw2-bs000.html +++ b/doc/Projects/2020/hw2/html/._hw2-bs000.html @@ -118,7 +118,7 @@ MathJax.Hub.Config({
Department of Physics, University of Oslo, Norway

-

Sep 1, 2020

+

Sep 8, 2020


@@ -206,19 +206,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -229,13 +229,13 @@ MSETrain = np.< lambdas = np.logspace(-4, 1, nlambdas) for i in range(nlambdas): lmb = lambdas[i] - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train # and then make the prediction - ytildeRidge = X_train @ Ridgebeta - ypredictRidge = X_test @ Ridgebeta + ytildeRidge = X_train @ Ridgebeta + ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSETrain[i] = MSE(y_train,ytildeRidge) -# Now plot the resulys +# Now plot the results plt.figure() plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train') plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test') @@ -309,19 +309,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -336,10 +336,10 @@ lambdas = np.# add ridge clf_ridge = skl.Ridge(alpha=lmb).fit(X_train, y_train) yridge = clf_ridge.predict(X_test) - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train # and then make the prediction - ytildeRidge = X_train @ Ridgebeta - ypredictRidge = X_test @ Ridgebeta + ytildeRidge = X_train @ Ridgebeta + ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSEPredictSKL[i] = MSE(y_test,yridge) MSETrain[i] = MSE(y_train,ytildeRidge) @@ -416,10 +416,10 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # The variance is given by the inverse of the matrix X^TX -print(np.linalg.inv(X_train.T @ X_train)) +print(np.linalg.inv(X_train.T @ X_train)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -431,8 +431,8 @@ MSETrain = np.< lambdas = np.logspace(-4, 0, nlambdas) for i in range(nlambdas): lmb = lambdas[i] - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train - print(np.linalg.inv(X_train.T @ X_train+lmb*I)) + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + print(np.linalg.inv(X_train.T @ X_train+lmb*I))

@@ -497,19 +497,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -718,7 +718,7 @@ x_train_scaled = scaler= scaler.transform(x_test) for degree in range(maxdegree): - model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) + model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) clf = model.fit(x_train_scale,y_train) y_fit = clf.predict(x_train_scaled) y_pred = clf.predict(x_test_scaled) diff --git a/doc/Projects/2020/hw2/html/hw2-bs.html b/doc/Projects/2020/hw2/html/hw2-bs.html index 847089b0c..a6e6c8963 100644 --- a/doc/Projects/2020/hw2/html/hw2-bs.html +++ b/doc/Projects/2020/hw2/html/hw2-bs.html @@ -118,7 +118,7 @@ MathJax.Hub.Config({
Department of Physics, University of Oslo, Norway

-

Sep 1, 2020

+

Sep 8, 2020


@@ -206,19 +206,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -229,13 +229,13 @@ MSETrain = np.< lambdas = np.logspace(-4, 1, nlambdas) for i in range(nlambdas): lmb = lambdas[i] - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train # and then make the prediction - ytildeRidge = X_train @ Ridgebeta - ypredictRidge = X_test @ Ridgebeta + ytildeRidge = X_train @ Ridgebeta + ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSETrain[i] = MSE(y_train,ytildeRidge) -# Now plot the resulys +# Now plot the results plt.figure() plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train') plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test') @@ -309,19 +309,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -336,10 +336,10 @@ lambdas = np.# add ridge clf_ridge = skl.Ridge(alpha=lmb).fit(X_train, y_train) yridge = clf_ridge.predict(X_test) - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train # and then make the prediction - ytildeRidge = X_train @ Ridgebeta - ypredictRidge = X_test @ Ridgebeta + ytildeRidge = X_train @ Ridgebeta + ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSEPredictSKL[i] = MSE(y_test,yridge) MSETrain[i] = MSE(y_train,ytildeRidge) @@ -416,10 +416,10 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # The variance is given by the inverse of the matrix X^TX -print(np.linalg.inv(X_train.T @ X_train)) +print(np.linalg.inv(X_train.T @ X_train)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -431,8 +431,8 @@ MSETrain = np.< lambdas = np.logspace(-4, 0, nlambdas) for i in range(nlambdas): lmb = lambdas[i] - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train - print(np.linalg.inv(X_train.T @ X_train+lmb*I)) + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + print(np.linalg.inv(X_train.T @ X_train+lmb*I))

@@ -497,19 +497,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -718,7 +718,7 @@ x_train_scaled = scaler= scaler.transform(x_test) for degree in range(maxdegree): - model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) + model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) clf = model.fit(x_train_scale,y_train) y_fit = clf.predict(x_train_scaled) y_pred = clf.predict(x_test_scaled) diff --git a/doc/Projects/2020/hw2/html/hw2.html b/doc/Projects/2020/hw2/html/hw2.html index bfbf4ed04..80a0bb896 100644 --- a/doc/Projects/2020/hw2/html/hw2.html +++ b/doc/Projects/2020/hw2/html/hw2.html @@ -85,7 +85,7 @@ MathJax.Hub.Config({
Department of Physics, University of Oslo, Norway

-

Sep 1, 2020

+

Sep 8, 2020


@@ -160,19 +160,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -183,13 +183,13 @@ MSETrain = np.< lambdas = np.logspace(-4, 1, nlambdas) for i in range(nlambdas): lmb = lambdas[i] - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train # and then make the prediction - ytildeRidge = X_train @ Ridgebeta - ypredictRidge = X_test @ Ridgebeta + ytildeRidge = X_train @ Ridgebeta + ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSETrain[i] = MSE(y_train,ytildeRidge) -# Now plot the resulys +# Now plot the results plt.figure() plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train') plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test') @@ -248,19 +248,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -275,10 +275,10 @@ lambdas = np.# add ridge clf_ridge = skl.Ridge(alpha=lmb).fit(X_train, y_train) yridge = clf_ridge.predict(X_test) - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train # and then make the prediction - ytildeRidge = X_train @ Ridgebeta - ypredictRidge = X_test @ Ridgebeta + ytildeRidge = X_train @ Ridgebeta + ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSEPredictSKL[i] = MSE(y_test,yridge) MSETrain[i] = MSE(y_train,ytildeRidge) @@ -341,10 +341,10 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # The variance is given by the inverse of the matrix X^TX -print(np.linalg.inv(X_train.T @ X_train)) +print(np.linalg.inv(X_train.T @ X_train)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -356,8 +356,8 @@ MSETrain = np.< lambdas = np.logspace(-4, 0, nlambdas) for i in range(nlambdas): lmb = lambdas[i] - Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train - print(np.linalg.inv(X_train.T @ X_train+lmb*I)) + Ridgebeta = np.linalg.inv(X_train.T @ X_train+lmb*I) @ X_train.T @ y_train + print(np.linalg.inv(X_train.T @ X_train+lmb*I))

@@ -408,19 +408,19 @@ X_train_scaled = scaler= scaler.transform(X_test) # matrix inversion to find beta -OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train -print(OLSbeta) +OLSbeta = np.linalg.inv(X_train.T @ X_train) @ X_train.T @ y_train +print(OLSbeta) # and then make the prediction -ytildeOLS = X_train @ OLSbeta -print("Training R2 for OLS") -print(R2(y_train,ytildeOLS)) -print("Training MSE for OLS") -print(MSE(y_train,ytildeOLS)) -ypredictOLS = X_test @ OLSbeta -print("Test R2 for OLS") -print(R2(y_test,ypredictOLS)) -print("Test MSE OLS") -print(MSE(y_test,ypredictOLS)) +ytildeOLS = X_train @ OLSbeta +print("Training R2 for OLS") +print(R2(y_train,ytildeOLS)) +print("Training MSE for OLS") +print(MSE(y_train,ytildeOLS)) +ypredictOLS = X_test @ OLSbeta +print("Test R2 for OLS") +print(R2(y_test,ypredictOLS)) +print("Test MSE OLS") +print(MSE(y_test,ypredictOLS)) # Repeat now for Ridge regression and various values of the regularization parameter I = np.eye(p,p) @@ -600,7 +600,7 @@ x_train_scaled = scaler= scaler.transform(x_test) for degree in range(maxdegree): - model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) + model = make_pipeline(PolynomialFeatures(degree=degree), LinearRegression(fit_intercept=False)) clf = model.fit(x_train_scale,y_train) y_fit = clf.predict(x_train_scaled) y_pred = clf.predict(x_test_scaled) diff --git a/doc/Projects/2020/hw2/ipynb/ipynb-hw2-src.tar.gz b/doc/Projects/2020/hw2/ipynb/ipynb-hw2-src.tar.gz index 9caf7c9eb..90b2e6d68 100644 Binary files a/doc/Projects/2020/hw2/ipynb/ipynb-hw2-src.tar.gz and b/doc/Projects/2020/hw2/ipynb/ipynb-hw2-src.tar.gz differ diff --git a/doc/Projects/2020/hw2/pdf/hw2.p.tex b/doc/Projects/2020/hw2/pdf/hw2.p.tex index 85004d1d1..812644e31 100644 --- a/doc/Projects/2020/hw2/pdf/hw2.p.tex +++ b/doc/Projects/2020/hw2/pdf/hw2.p.tex @@ -171,7 +171,7 @@ Homework 2, weeks 36 and 37 % --- begin date --- \begin{center} -Sep 1, 2020 +Sep 8, 2020 \end{center} % --- end date --- @@ -276,7 +276,7 @@ for i in range(nlambdas): ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSETrain[i] = MSE(y_train,ytildeRidge) -# Now plot the resulys +# Now plot the results plt.figure() plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train') plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test') diff --git a/doc/Projects/2020/hw2/pdf/hw2.pdf b/doc/Projects/2020/hw2/pdf/hw2.pdf index de91d0a65..bc2cdf423 100644 Binary files a/doc/Projects/2020/hw2/pdf/hw2.pdf and b/doc/Projects/2020/hw2/pdf/hw2.pdf differ diff --git a/doc/Projects/2020/hw2/pdf/hw2.tex b/doc/Projects/2020/hw2/pdf/hw2.tex index cc3c2b843..68d65edce 100644 --- a/doc/Projects/2020/hw2/pdf/hw2.tex +++ b/doc/Projects/2020/hw2/pdf/hw2.tex @@ -141,7 +141,7 @@ Homework 2, weeks 36 and 37 % --- begin date --- \begin{center} -Sep 1, 2020 +Sep 8, 2020 \end{center} % --- end date --- @@ -167,10 +167,10 @@ distribution. The function $y$ is a quadratic polynomial in $x$ with added stochastic noise according to the normal distribution $\cal{N}(0,1)$. The following simple Python instructions define our $x$ and $y$ values (with 100 data points). -\begin{verbatim} +\begin{print} x = np.random.rand(100) y = 2.0+5*x*x+0.1*np.random.randn(100) -\end{verbatim} +\end{print} \subex{a)} @@ -180,7 +180,7 @@ Write your own code for the Ridge method (see chapter 3.4 of Hastie \emph{et al. % --- begin solution of exercise --- \paragraph{Solution.} The code here allows you to perform your own Ridge calculation and perform calculations for various values of the regularization parameter $\lambda$. This program can easily be extended upon. -\begin{verbatim} +\begin{print} import os import numpy as np import pandas as pd @@ -246,7 +246,7 @@ for i in range(nlambdas): ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSETrain[i] = MSE(y_train,ytildeRidge) -# Now plot the resulys +# Now plot the results plt.figure() plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train') plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test') @@ -254,7 +254,7 @@ plt.xlabel('log10(lambda)') plt.ylabel('MSE') plt.legend() plt.show() -\end{verbatim} +\end{print} % --- end solution of exercise --- @@ -265,7 +265,7 @@ Repeat the above but using the functionality of \textbf{Scikit-Learn}. Compare y % --- begin solution of exercise --- \paragraph{Solution.} To use \textbf{scikit-learn} with Ridge, we simply need to add the relevant function \textbf{Ridge()}, as done in the code here. -\begin{verbatim} +\begin{print} import numpy as np import pandas as pd import matplotlib.pyplot as plt @@ -345,7 +345,7 @@ plt.xlabel('log10(lambda)') plt.ylabel('MSE') plt.legend() plt.show() -\end{verbatim} +\end{print} % --- end solution of exercise --- @@ -355,7 +355,7 @@ Our next step is to study the variance of the parameters $\beta_1$ and $\beta_2$ % --- begin solution of exercise --- \paragraph{Solution.} -\begin{verbatim} +\begin{print} import numpy as np import pandas as pd import matplotlib.pyplot as plt @@ -412,7 +412,7 @@ for i in range(nlambdas): -\end{verbatim} +\end{print} % --- end solution of exercise --- @@ -422,7 +422,7 @@ Repeat the previous step but add now the Lasso method, see equation (3.53) of Ha % --- begin solution of exercise --- \paragraph{Solution.} -\begin{verbatim} +\begin{print} import numpy as np import pandas as pd import matplotlib.pyplot as plt @@ -497,7 +497,7 @@ plt.xlabel('log10(lambda)') plt.ylabel('MSE') plt.legend() plt.show() -\end{verbatim} +\end{print} % --- end solution of exercise --- @@ -574,17 +574,17 @@ techniques. It also common to split the data in a \textbf{training} set and a \textbf{testing} set. A typical split is to use $80\%$ of the data for training and the rest for testing. This can be done as follows with our design matrix $\bm{X}$ and data $\bm{y}$ (remember to import \textbf{scikit-learn}) -\begin{verbatim} +\begin{print} # split in training and test data X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2) -\end{verbatim} +\end{print} Then we can use the standard scaler to scale our data as -\begin{verbatim} +\begin{print} scaler = StandardScaler() scaler.fit(X_train) X_train_scaled = scaler.transform(X_train) X_test_scaled = scaler.transform(X_test) -\end{verbatim} +\end{print} In this exercise we want you to to compute the MSE for the training @@ -597,14 +597,14 @@ We will also use Ridge and Lasso regression. Our data is defined by $x\in [-3,3]$ with a total of for example $100$ data points. -\begin{verbatim} +\begin{print} 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) -\end{verbatim} +\end{print} where $y$ is the function we want to fit with a given polynomial. @@ -614,7 +614,7 @@ Write a first code which sets up a design matrix $X$ defined by a fifth-order po % --- begin solution of exercise --- \paragraph{Solution.} -\begin{verbatim} +\begin{print} import matplotlib.pyplot as plt import numpy as np from sklearn.linear_model import LinearRegression, Ridge, Lasso @@ -651,7 +651,7 @@ plt.plot(polydegree, TestError, label='Test Error') plt.plot(polydegree, TrainError, label='Train Error') plt.legend() plt.show() -\end{verbatim} +\end{print} % --- end solution of exercise --- @@ -682,7 +682,7 @@ Repeat part (2c) but now using Ridge regressions with various hyperparameters $\ % --- begin solution of exercise --- \paragraph{Solution.} Here you need to add for example the same loop over the parameters $\lambda$ as you did in the first exercise, that is add -\begin{verbatim} +\begin{print} nlambdas = 100 MSEPredictRidge = np.zeros(nlambdas) lambdas = np.logspace(-4, 0, nlambdas) @@ -691,7 +691,7 @@ for i in range(nlambdas): # add ridge clf_ridge = skl.Ridge(alpha=lmb).fit(X_train_scaled, y_train) -\end{verbatim} +\end{print} The plotting functionality of the first exercise can be reused here as well. % --- end solution of exercise --- diff --git a/doc/src/Projects/2020/Exercises/hw2.do.txt b/doc/src/Projects/2020/Exercises/hw2.do.txt index c63c6d889..f55082a55 100644 --- a/doc/src/Projects/2020/Exercises/hw2.do.txt +++ b/doc/src/Projects/2020/Exercises/hw2.do.txt @@ -94,7 +94,7 @@ for i in range(nlambdas): ypredictRidge = X_test @ Ridgebeta MSEPredict[i] = MSE(y_test,ypredictRidge) MSETrain[i] = MSE(y_train,ytildeRidge) -# Now plot the resulys +# Now plot the results plt.figure() plt.plot(np.log10(lambdas), MSETrain, label = 'MSE Ridge train') plt.plot(np.log10(lambdas), MSEPredict, 'r--', label = 'MSE Ridge Test') diff --git a/doc/src/Projects/2020/Exercises/make.sh b/doc/src/Projects/2020/Exercises/make.sh index 740fedbc3..e9d98a525 100755 --- a/doc/src/Projects/2020/Exercises/make.sh +++ b/doc/src/Projects/2020/Exercises/make.sh @@ -35,6 +35,9 @@ html=${name}-bs system doconce format html $name --html_style=bootstrap --pygments_html_style=default --html_admon=bootstrap_panel --html_output=$html $opt system doconce split_html $html.html --method=split --pagination --nav_button=bottom +# IPython notebook +system doconce format ipynb $name $opt + # Ordinary plain LaTeX document system doconce format pdflatex $name --print_latex_style=trac --latex_admon=paragraph $opt @@ -77,3 +80,7 @@ EOF tar czf ${ipynb_tarfile} README.txt fi cp ${ipynb_tarfile} $dest/$name/ipynb + + + +