Get startedGet started for free

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.

This exercise is part of the course

Supervised Learning with scikit-learn

View Course

Exercise instructions

  • Import Ridge.
  • Instantiate Ridge, setting alpha equal to alpha.
  • Fit the model to the training data.
  • Calculate the \(R^2\) score for each iteration of ridge.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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)
Edit and Run Code