36 lines
1.1 KiB
Python
36 lines
1.1 KiB
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
|
|
|
|
# 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 = 500
|
|
lambdas = np.logspace(-3, 5, nlambdas)
|
|
# Initialize a KFold instance
|
|
k = 5
|
|
kfold = KFold(n_splits = k)
|
|
estimated_mse_sklearn = np.zeros(nlambdas)
|
|
i = 0
|
|
for lmb in lambdas:
|
|
ridge = Ridge(alpha = lmb)
|
|
estimated_mse_folds = cross_val_score(ridge, x, y, scoring='neg_mean_squared_error', cv=kfold)
|
|
estimated_mse_sklearn[i] = np.mean(-estimated_mse_folds)
|
|
i += 1
|
|
plt.figure()
|
|
plt.plot(np.log10(lambdas), estimated_mse_sklearn, label = 'cross_val_score')
|
|
plt.xlabel('log10(lambda)')
|
|
plt.ylabel('MSE')
|
|
plt.legend()
|
|
plt.show()
|