Last active
May 24, 2020 01:37
-
-
Save gandroz/60c64bfce9a43d76937a00eac335def2 to your computer and use it in GitHub Desktop.
Ridge regression
This file contains hidden or 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
def ridge_regression(data, predictors, alpha=None): | |
# Fit the model | |
if alpha is not None and predictors is not None: | |
model = Ridge(alpha=alpha, normalize=True, max_iter=1e8) | |
else: | |
model = RidgeCV(alphas=np.linspace(1e-4, 1e-3, 1000), fit_intercept=True, normalize=True) | |
model.fit(data[predictors],data['y']) | |
y_pred = model.predict(data[predictors]) | |
# Plot the results | |
data_pred = data | |
data_pred['y_pred'] = y_pred | |
if alpha is not None: | |
title = '{:.2e}'.format(alpha) | |
else: | |
title = None | |
graph = generate_plot(data_pred, title=title) | |
return graph, model | |
# Initialize predictors to all 15 powers of x | |
predictors=['x'] | |
predictors.extend(['x_{}'.format(i) for i in range(2,16)]) | |
# Define the alpha values to test | |
alpha_ridge = [1e-8, 1e-5, 1e-4, 1e-3, 1e-2, 1] | |
# Iterate over the 10 alpha values: | |
graphs = [] | |
data - get_data() | |
for i in range(6): | |
graph, _ = ridge_regression(data, predictors, alpha_ridge[i]) | |
graphs.append(graph) | |
grid = gridplot([[graphs[0], graphs[1]], [graphs[2], graphs[3]], [graphs[4], graphs[5]]]) | |
show(grid); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment