Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ychennay
Last active May 11, 2019 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ychennay/59091f6a31e88f8e2064910e37ee1101 to your computer and use it in GitHub Desktop.
Save ychennay/59091f6a31e88f8e2064910e37ee1101 to your computer and use it in GitHub Desktop.
# generate 100 different models with different lambda values, from 0 to 50
lambdas = np.linspace(0, 50, 200)
bias = []
variance = []
for λ in lambdas:
# refit model with new λ hyperparameter
β = np.linalg.inv(X.T.dot(X) + np.diag([λ] * len(X.T))).dot(X.T).dot(y).flatten()
bias.append(np.mean(np.mean(np.abs(X.dot(β) - y.values.flatten()))))
variance.append(np.var(X.dot(β)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment