31 lines
979 B
Python
31 lines
979 B
Python
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
from sklearn.model_selection import KFold
|
|
from sklearn.linear_model import Ridge
|
|
from sklearn.model_selection import cross_val_score
|
|
from sklearn.preprocessing import PolynomialFeatures
|
|
from sklearn.model_selection import GridSearchCV
|
|
|
|
# A seed just to ensure that the random numbers are the same for every run.
|
|
np.random.seed(3155)
|
|
# Generate the data.
|
|
n = 100
|
|
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)
|
|
# Decide degree on polynomial to fit
|
|
poly = PolynomialFeatures(degree = 10)
|
|
|
|
# Decide which values of lambda to use
|
|
nlambdas = 10
|
|
lambdas = np.logspace(-3, 3, nlambdas)
|
|
|
|
# create and fit a ridge regression model, testing each alpha
|
|
model = Ridge()
|
|
grid = GridSearchCV(estimator=model, param_grid=dict(alpha=lambdas))
|
|
grid.fit(x, y)
|
|
print(grid)
|
|
# summarize the results of the grid search
|
|
print(grid.best_score_)
|
|
print(grid.best_estimator_.alpha)
|
|
|