Get Started

Hyperparameter tuning with GridSearchCV

Now you have seen how to perform grid search hyperparameter tuning, you are going to build a lasso regression model with optimal hyperparameters to predict blood glucose levels using the features in the diabetes_df dataset.

X_train, X_test, y_train, and y_test have been preloaded for you. A KFold() object has been created and stored for you as kf, along with a lasso regression model as lasso.

This is a part of the course

“Supervised Learning with scikit-learn”

View Course

Exercise instructions

  • Import GridSearchCV.
  • Set up a parameter grid for "alpha", using np.linspace() to create 20 evenly spaced values ranging from 0.00001 to 1.
  • Call GridSearchCV(), passing lasso, the parameter grid, and setting cv equal to kf.
  • Fit the grid search object to the training data to perform a cross-validated grid search.

Hands-on interactive exercise

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

# Import GridSearchCV
____

# Set up the parameter grid
param_grid = {"____": np.linspace(____, ____, ____)}

# Instantiate lasso_cv
lasso_cv = ____(____, ____, cv=____)

# Fit to the training data
____
print("Tuned lasso paramaters: {}".format(lasso_cv.best_params_))
print("Tuned lasso score: {}".format(lasso_cv.best_score_))
Edit and Run Code