diff --git a/doc/pub/week38/html/._week38-bs000.html b/doc/pub/week38/html/._week38-bs000.html index f40e5396c..ea3f3b366 100644 --- a/doc/pub/week38/html/._week38-bs000.html +++ b/doc/pub/week38/html/._week38-bs000.html @@ -253,7 +253,7 @@ MathJax.Hub.Config({
-
diff --git a/doc/pub/week38/html/._week38-bs007.html b/doc/pub/week38/html/._week38-bs007.html index 7cdbb1c01..08007745f 100644 --- a/doc/pub/week38/html/._week38-bs007.html +++ b/doc/pub/week38/html/._week38-bs007.html @@ -238,7 +238,7 @@ MathJax.Hub.Config({
When you are comparing your own code with for example Scikit-Learn's library, there are some minor things to keep in mind. -The example here shows how one can leave out or keep the intercept. +The example here shows how one can keep the intercept in order to compare own code.
@@ -319,10 +319,10 @@ lambdas = np.
print(RegRidge.coef_)
# Now plot the results
plt.figure()
-plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r--', label = 'MSE Ridge Test')
-plt.plot(np.log10(lambdas), MSERidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, 'b', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSERidgeTrain, 'y', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test')
plt.xlabel('log10(lambda)')
plt.ylabel('MSE')
diff --git a/doc/pub/week38/html/week38-bs.html b/doc/pub/week38/html/week38-bs.html
index f40e5396c..ea3f3b366 100644
--- a/doc/pub/week38/html/week38-bs.html
+++ b/doc/pub/week38/html/week38-bs.html
@@ -253,7 +253,7 @@ MathJax.Hub.Config({
-
diff --git a/doc/pub/week38/html/week38-reveal.html b/doc/pub/week38/html/week38-reveal.html
index e822d9758..261aba841 100644
--- a/doc/pub/week38/html/week38-reveal.html
+++ b/doc/pub/week38/html/week38-reveal.html
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
@@ -423,7 +423,7 @@ plt.show()
When you are comparing your own code with for example Scikit-Learn's library, there are some minor things to keep in mind.
-The example here shows how one can leave out or keep the intercept.
+The example here shows how one can keep the intercept in order to compare own code.
@@ -504,10 +504,10 @@ lambdas = np.logspace(-4, print(RegRidge.coef_)
# Now plot the results
plt.figure()
-plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r--', label = 'MSE Ridge Test')
-plt.plot(np.log10(lambdas), MSERidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, 'b', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSERidgeTrain, 'y', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test')
plt.xlabel('log10(lambda)')
plt.ylabel('MSE')
diff --git a/doc/pub/week38/html/week38-solarized.html b/doc/pub/week38/html/week38-solarized.html
index 10c67f7b6..0c7d4a431 100644
--- a/doc/pub/week38/html/week38-solarized.html
+++ b/doc/pub/week38/html/week38-solarized.html
@@ -179,7 +179,7 @@ MathJax.Hub.Config({
-
When you are comparing your own code with for example Scikit-Learn's library, there are some minor things to keep in mind.
-The example here shows how one can leave out or keep the intercept.
+The example here shows how one can keep the intercept in order to compare own code.
@@ -511,10 +511,10 @@ lambdas = np.logspace(-4, print(RegRidge.coef_)
# Now plot the results
plt.figure()
-plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r--', label = 'MSE Ridge Test')
-plt.plot(np.log10(lambdas), MSERidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, 'b', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSERidgeTrain, 'y', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test')
plt.xlabel('log10(lambda)')
plt.ylabel('MSE')
diff --git a/doc/pub/week38/html/week38.html b/doc/pub/week38/html/week38.html
index 90b0585ae..dfcb42880 100644
--- a/doc/pub/week38/html/week38.html
+++ b/doc/pub/week38/html/week38.html
@@ -184,7 +184,7 @@ MathJax.Hub.Config({
-
When you are comparing your own code with for example Scikit-Learn's library, there are some minor things to keep in mind.
-The example here shows how one can leave out or keep the intercept.
+The example here shows how one can keep the intercept in order to compare own code.
@@ -516,10 +516,10 @@ lambdas = np.
print(RegRidge.coef_)
# Now plot the results
plt.figure()
-plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r--', label = 'MSE Ridge Test')
-plt.plot(np.log10(lambdas), MSERidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, 'b', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSERidgeTrain, 'y', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test')
plt.xlabel('log10(lambda)')
plt.ylabel('MSE')
diff --git a/doc/pub/week38/ipynb/ipynb-week38-src.tar.gz b/doc/pub/week38/ipynb/ipynb-week38-src.tar.gz
index 9ae162997..f5c6a0d61 100644
Binary files a/doc/pub/week38/ipynb/ipynb-week38-src.tar.gz and b/doc/pub/week38/ipynb/ipynb-week38-src.tar.gz differ
diff --git a/doc/pub/week38/ipynb/week38.ipynb b/doc/pub/week38/ipynb/week38.ipynb
index d6dd2c4ec..142984b8c 100644
--- a/doc/pub/week38/ipynb/week38.ipynb
+++ b/doc/pub/week38/ipynb/week38.ipynb
@@ -10,7 +10,7 @@
" \n",
"**Morten Hjorth-Jensen**, Department of Physics, University of Oslo and Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University\n",
"\n",
- "Date: **Sep 20, 2021**\n",
+ "Date: **Sep 21, 2021**\n",
"\n",
"Copyright 1999-2021, Morten Hjorth-Jensen. Released under CC Attribution-NonCommercial 4.0 license\n",
"\n",
@@ -350,7 +350,7 @@
"## To think about\n",
"\n",
"When you are comparing your own code with for example **Scikit-Learn**'s library, there are some minor things to keep in mind.\n",
- "The example here shows how one can leave out or keep the intercept."
+ "The example here shows how one can keep the intercept in order to compare own code."
]
},
{
@@ -438,10 +438,10 @@
" print(RegRidge.coef_)\n",
"# Now plot the results\n",
"plt.figure()\n",
- "plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, label = 'MSE Ridge train')\n",
- "plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r--', label = 'MSE Ridge Test')\n",
- "plt.plot(np.log10(lambdas), MSERidgeTrain, label = 'MSE Ridge train')\n",
- "plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Test')\n",
+ "plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, 'b', label = 'MSE Ridge train')\n",
+ "plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE Ridge Test')\n",
+ "plt.plot(np.log10(lambdas), MSERidgeTrain, 'y', label = 'MSE Ridge train')\n",
+ "plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test')\n",
"\n",
"plt.xlabel('log10(lambda)')\n",
"plt.ylabel('MSE')\n",
diff --git a/doc/src/week36/scale.py b/doc/src/week36/scale.py
new file mode 100644
index 000000000..0acdb1a2b
--- /dev/null
+++ b/doc/src/week36/scale.py
@@ -0,0 +1,85 @@
+import numpy as np
+import pandas as pd
+import matplotlib.pyplot as plt
+from sklearn.model_selection import train_test_split
+from sklearn import linear_model
+from sklearn.preprocessing import StandardScaler
+
+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
+
+
+# A seed just to ensure that the random numbers are the same for every run.
+# Useful for eventual debugging.
+np.random.seed(3155)
+
+n = 10
+x = np.random.rand(n)
+y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)
+
+Maxpolydegree = 5
+X = np.zeros((n,Maxpolydegree))
+X[:,0] = 1.0
+
+for polydegree in range(1, Maxpolydegree):
+ for degree in range(polydegree):
+ X[:,degree] = x**(degree)
+
+
+
+
+# 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)
+
+
+
+# Do not scale by std
+scaler = StandardScaler(with_std=False)
+scaler.fit(X_train)
+X_train_scaled = scaler.transform(X_train)
+X_test_scaled = scaler.transform(X_test)
+
+#X_train_scaled = X_train
+#X_test_scaled = X_test
+
+
+
+p = Maxpolydegree
+I = np.eye(p,p)
+# Decide which values of lambda to use
+nlambdas = 2
+MSEOwnRidgePredict = np.zeros(nlambdas)
+MSERidgePredict = np.zeros(nlambdas)
+
+lambdas = np.logspace(-4, 1, nlambdas)
+for i in range(nlambdas):
+ lmb = lambdas[i]
+ OwnRidgeBeta = np.linalg.pinv(X_train_scaled.T @ X_train_scaled+lmb*I) @ X_train_scaled.T @ y_train
+ RegRidge = linear_model.Ridge(lmb,fit_intercept=False)#True, normalize=False)
+ RegRidge.fit(X_train_scaled,y_train)
+ ypredictOwnRidge = X_test_scaled @ OwnRidgeBeta
+ print("Values for own Ridge prediction")
+ print(ypredictOwnRidge)
+ ypredictRidge = RegRidge.predict(X_test_scaled)
+ print("Values for SL Ridge prediction")
+ print(ypredictRidge)
+ MSEOwnRidgePredict[i] = MSE(y_test,ypredictOwnRidge)
+ MSERidgePredict[i] = MSE(y_test,ypredictRidge)
+ print("Beta values for own Ridge implementation")
+ print(OwnRidgeBeta)
+ print("Beta values for Scikit-Learn Ridge implementation")
+ print(RegRidge.coef_)
+# Now plot the results
+"""
+plt.figure()
+plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'b--', label = 'MSE own Ridge Test')
+plt.plot(np.log10(lambdas), MSERidgePredict, 'g--', label = 'MSE SL Ridge Test')
+
+plt.xlabel('log10(lambda)')
+plt.ylabel('MSE')
+plt.legend()
+plt.show()
+"""
diff --git a/doc/src/week36/scaler.py b/doc/src/week36/scaler.py
new file mode 100644
index 000000000..4a123a221
--- /dev/null
+++ b/doc/src/week36/scaler.py
@@ -0,0 +1,8 @@
+from sklearn.preprocessing import StandardScaler
+data = [[0, 0], [0, 0], [1, 1], [1, 1]]
+scaler = StandardScaler()
+print(scaler.fit(data))
+StandardScaler()
+print(scaler.mean_)
+print(scaler.transform(data))
+print(scaler.transform([[2, 2]]))
diff --git a/doc/src/week38/week38.do.txt b/doc/src/week38/week38.do.txt
index 0b6311775..c51b19c4b 100644
--- a/doc/src/week38/week38.do.txt
+++ b/doc/src/week38/week38.do.txt
@@ -237,7 +237,7 @@ plt.show()
===== To think about =====
When you are comparing your own code with for example _Scikit-Learn_'s library, there are some minor things to keep in mind.
-The example here shows how one can leave out or keep the intercept.
+The example here shows how one can keep the intercept in order to compare own code.
!bc pycod
import numpy as np
@@ -316,10 +316,10 @@ for i in range(nlambdas):
print(RegRidge.coef_)
# Now plot the results
plt.figure()
-plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r--', label = 'MSE Ridge Test')
-plt.plot(np.log10(lambdas), MSERidgeTrain, label = 'MSE Ridge train')
-plt.plot(np.log10(lambdas), MSERidgePredict, 'r--', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, 'b', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSEOwnRidgePredict, 'r', label = 'MSE Ridge Test')
+plt.plot(np.log10(lambdas), MSERidgeTrain, 'y', label = 'MSE Ridge train')
+plt.plot(np.log10(lambdas), MSERidgePredict, 'g', label = 'MSE Ridge Test')
plt.xlabel('log10(lambda)')
plt.ylabel('MSE')
Sep 20, 2021
Sep 21, 2021
-Sep 20, 2021
Sep 21, 2021
Sep 20, 2021
Sep 21, 2021
@@ -430,7 +430,7 @@ plt.show()
Sep 20, 2021
Sep 21, 2021
@@ -435,7 +435,7 @@ plt.show()