testting codes

This commit is contained in:
Morten Hjorth-Jensen
2021-09-21 10:54:28 +02:00
parent ca4dc3db8b
commit 249454d2dc
11 changed files with 129 additions and 36 deletions
+85
View File
@@ -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()
"""
+8
View File
@@ -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]]))
+5 -5
View File
@@ -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')