Skip to content

Instantly share code, notes, and snippets.

@oliverangelil
Last active October 13, 2021 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oliverangelil/c40a39202cc3f1d9f53435f18f713abb to your computer and use it in GitHub Desktop.
Save oliverangelil/c40a39202cc3f1d9f53435f18f713abb 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: https://oliverangelil.github.io/posts/bias-variance
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