Regularized regression: Ridge
Ridge regression performs regularization by computing the squared values of the model parameters multiplied by alpha and adding them to the loss function.
In this exercise, you will fit ridge regression models over a range of different alpha values, and print their \(R^2\) scores. You will use all of the features in the sales_df dataset to predict "sales". The data has been split into X_train, X_test, y_train, y_test for you.
A variable called alphas has been provided as a list containing different alpha values, which you will loop through to generate scores.
Deze oefening maakt deel uit van de cursus
Supervised Learning with scikit-learn
Oefeninstructies
- Import
Ridge. - Instantiate
Ridge, setting alpha equal toalpha. - Fit the model to the training data.
- Calculate the \(R^2\) score for each iteration of
ridge.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# Import Ridge
from ____.____ import ____
alphas = [0.1, 1.0, 10.0, 100.0, 1000.0, 10000.0]
ridge_scores = []
for alpha in alphas:
# Create a Ridge regression model
ridge = ____
# Fit the data
____
# Obtain R-squared
score = ____
ridge_scores.append(score)
print(ridge_scores)