-
-
Save ebakan/528524bf155697d17dbaadfb82d3830e to your computer and use it in GitHub Desktop.
generate dummy data and apply ridge with varying levels of regularisation. Plots train and test results. This code goes with my blog post: http://snoek.ddns.net/~oliver/mysite/the-bias-variance-tradeoff.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.datasets import make_regression | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import Ridge | |
from sklearn.metrics import mean_squared_error | |
import numpy as np | |
import matplotlib.pyplot as plt | |
# generate data | |
X, y, w = make_regression(n_samples=1000, n_features=200, coef=True, | |
random_state=1, bias=0, noise=3, tail_strength=0.9, effective_rank=10) | |
# 60/40 split on train/test data | |
X_train, X_test, y_train, y_test = train_test_split( | |
X, y, test_size=0.4, random_state=1) | |
# list of alphas | |
alphas = 10.**np.arange(-4, 3, 0.1) | |
# loop through alphas, fit model, extract train and test results | |
scores_test = [] | |
scores_train = [] | |
for a in alphas: | |
print(a) | |
clf = Ridge(alpha=a, solver='auto') | |
clf.fit(X_train, y_train) | |
scores_train.append(mean_squared_error(y_train, clf.predict(X_train))) | |
scores_test.append(mean_squared_error(y_test, clf.predict(X_test))) | |
# plot MSE for train and test sets | |
fig, ax = plt.subplots(figsize=(15, 7)) | |
ax.set_xscale('log') | |
ax.plot(alphas, scores_test, 'r', label='test') | |
ax.plot(alphas, scores_train, 'b', label='train') | |
ax.set_ylabel('MSE', fontsize=16) | |
ax.set_xlabel('alpha (regularisation strength)', fontsize=16) | |
ax.legend(fontsize=18) | |
plt.ion(); plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment