testting codes
This commit is contained in:
@@ -253,7 +253,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 20, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 21, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
|
||||
@@ -238,7 +238,7 @@ MathJax.Hub.Config({
|
||||
|
||||
<p>
|
||||
When you are comparing your own code with for example <b>Scikit-Learn</b>'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.
|
||||
|
||||
<p>
|
||||
|
||||
@@ -319,10 +319,10 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
<span style="color: #008000">print</span>(RegRidge<span style="color: #666666">.</span>coef_)
|
||||
<span style="color: #408080; font-style: italic"># Now plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgeTrain, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge train'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgePredict, <span style="color: #BA2121">'r--'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge Test'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgeTrain, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge train'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgePredict, <span style="color: #BA2121">'r--'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge Test'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgeTrain, <span style="color: #BA2121">'b'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge train'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgePredict, <span style="color: #BA2121">'r'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge Test'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgeTrain, <span style="color: #BA2121">'y'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge train'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgePredict, <span style="color: #BA2121">'g'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge Test'</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">'log10(lambda)'</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">'MSE'</span>)
|
||||
|
||||
@@ -253,7 +253,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 20, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 21, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
|
||||
@@ -148,7 +148,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p> <br>
|
||||
<center><h4>Sep 20, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 21, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
|
||||
@@ -423,7 +423,7 @@ plt.show()
|
||||
|
||||
<p>
|
||||
When you are comparing your own code with for example <b>Scikit-Learn</b>'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.
|
||||
|
||||
<p>
|
||||
|
||||
@@ -504,10 +504,10 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
<span style="color: #658b00">print</span>(RegRidge.coef_)
|
||||
<span style="color: #228B22"># Now plot the results</span>
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, label = <span style="color: #CD5555">'MSE Ridge train'</span>)
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, <span style="color: #CD5555">'r--'</span>, label = <span style="color: #CD5555">'MSE Ridge Test'</span>)
|
||||
plt.plot(np.log10(lambdas), MSERidgeTrain, label = <span style="color: #CD5555">'MSE Ridge train'</span>)
|
||||
plt.plot(np.log10(lambdas), MSERidgePredict, <span style="color: #CD5555">'r--'</span>, label = <span style="color: #CD5555">'MSE Ridge Test'</span>)
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, <span style="color: #CD5555">'b'</span>, label = <span style="color: #CD5555">'MSE Ridge train'</span>)
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, <span style="color: #CD5555">'r'</span>, label = <span style="color: #CD5555">'MSE Ridge Test'</span>)
|
||||
plt.plot(np.log10(lambdas), MSERidgeTrain, <span style="color: #CD5555">'y'</span>, label = <span style="color: #CD5555">'MSE Ridge train'</span>)
|
||||
plt.plot(np.log10(lambdas), MSERidgePredict, <span style="color: #CD5555">'g'</span>, label = <span style="color: #CD5555">'MSE Ridge Test'</span>)
|
||||
|
||||
plt.xlabel(<span style="color: #CD5555">'log10(lambda)'</span>)
|
||||
plt.ylabel(<span style="color: #CD5555">'MSE'</span>)
|
||||
|
||||
@@ -179,7 +179,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 20, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 21, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -430,7 +430,7 @@ plt.show()
|
||||
|
||||
<p>
|
||||
When you are comparing your own code with for example <b>Scikit-Learn</b>'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.
|
||||
|
||||
<p>
|
||||
|
||||
@@ -511,10 +511,10 @@ lambdas = np.logspace(-<span style="color: #B452CD">4</span>, <span style="color
|
||||
<span style="color: #658b00">print</span>(RegRidge.coef_)
|
||||
<span style="color: #228B22"># Now plot the results</span>
|
||||
plt.figure()
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, label = <span style="color: #CD5555">'MSE Ridge train'</span>)
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, <span style="color: #CD5555">'r--'</span>, label = <span style="color: #CD5555">'MSE Ridge Test'</span>)
|
||||
plt.plot(np.log10(lambdas), MSERidgeTrain, label = <span style="color: #CD5555">'MSE Ridge train'</span>)
|
||||
plt.plot(np.log10(lambdas), MSERidgePredict, <span style="color: #CD5555">'r--'</span>, label = <span style="color: #CD5555">'MSE Ridge Test'</span>)
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgeTrain, <span style="color: #CD5555">'b'</span>, label = <span style="color: #CD5555">'MSE Ridge train'</span>)
|
||||
plt.plot(np.log10(lambdas), MSEOwnRidgePredict, <span style="color: #CD5555">'r'</span>, label = <span style="color: #CD5555">'MSE Ridge Test'</span>)
|
||||
plt.plot(np.log10(lambdas), MSERidgeTrain, <span style="color: #CD5555">'y'</span>, label = <span style="color: #CD5555">'MSE Ridge train'</span>)
|
||||
plt.plot(np.log10(lambdas), MSERidgePredict, <span style="color: #CD5555">'g'</span>, label = <span style="color: #CD5555">'MSE Ridge Test'</span>)
|
||||
|
||||
plt.xlabel(<span style="color: #CD5555">'log10(lambda)'</span>)
|
||||
plt.ylabel(<span style="color: #CD5555">'MSE'</span>)
|
||||
|
||||
@@ -184,7 +184,7 @@ MathJax.Hub.Config({
|
||||
<center>[2] <b>Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University</b></center>
|
||||
<br>
|
||||
<p>
|
||||
<center><h4>Sep 20, 2021</h4></center> <!-- date -->
|
||||
<center><h4>Sep 21, 2021</h4></center> <!-- date -->
|
||||
<br>
|
||||
<p>
|
||||
<!-- !split --><br><br><br><br><br><br><br><br><br><br>
|
||||
@@ -435,7 +435,7 @@ plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<p>
|
||||
When you are comparing your own code with for example <b>Scikit-Learn</b>'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.
|
||||
|
||||
<p>
|
||||
|
||||
@@ -516,10 +516,10 @@ lambdas <span style="color: #666666">=</span> np<span style="color: #666666">.</
|
||||
<span style="color: #008000">print</span>(RegRidge<span style="color: #666666">.</span>coef_)
|
||||
<span style="color: #408080; font-style: italic"># Now plot the results</span>
|
||||
plt<span style="color: #666666">.</span>figure()
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgeTrain, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge train'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgePredict, <span style="color: #BA2121">'r--'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge Test'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgeTrain, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge train'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgePredict, <span style="color: #BA2121">'r--'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge Test'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgeTrain, <span style="color: #BA2121">'b'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge train'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSEOwnRidgePredict, <span style="color: #BA2121">'r'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge Test'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgeTrain, <span style="color: #BA2121">'y'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge train'</span>)
|
||||
plt<span style="color: #666666">.</span>plot(np<span style="color: #666666">.</span>log10(lambdas), MSERidgePredict, <span style="color: #BA2121">'g'</span>, label <span style="color: #666666">=</span> <span style="color: #BA2121">'MSE Ridge Test'</span>)
|
||||
|
||||
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">'log10(lambda)'</span>)
|
||||
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">'MSE'</span>)
|
||||
|
||||
Binary file not shown.
@@ -10,7 +10,7 @@
|
||||
"<!-- Author: --> \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",
|
||||
|
||||
@@ -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()
|
||||
"""
|
||||
@@ -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]]))
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user