44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
import numpy as np
|
|
from sklearn.model_selection import train_test_split
|
|
from sklearn.linear_model import Ridge
|
|
from sklearn.model_selection import GridSearchCV
|
|
|
|
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(2021)
|
|
|
|
n = 100
|
|
x = np.random.rand(n)
|
|
y = np.exp(-x**2) + 1.5 * np.exp(-(x-2)**2)
|
|
|
|
Maxpolydegree = 5
|
|
X = np.zeros((n,Maxpolydegree-1))
|
|
|
|
for degree in range(1,Maxpolydegree): #No intercept column
|
|
X[:,degree-1] = 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)
|
|
|
|
# Decide which values of lambda to use
|
|
nlambdas = 10
|
|
lambdas = np.logspace(-4, 2, nlambdas)
|
|
# create and fit a ridge regression model, testing each alpha
|
|
model = Ridge()
|
|
gridsearch = GridSearchCV(estimator=model, param_grid=dict(alpha=lambdas))
|
|
gridsearch.fit(X_train, y_train)
|
|
print(gridsearch)
|
|
ypredictRidge = gridsearch.predict(X_test)
|
|
# summarize the results of the grid search
|
|
#print(gridsearch.best_score_)
|
|
print(f"Best estimated lambda-value: {gridsearch.best_estimator_.alpha}")
|
|
print(f"MSE score: {MSE(y_test,ypredictRidge)}")
|
|
print(f"R2 score: {R2(y_test,ypredictRidge)}")
|