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”
Exercise instructions
- Import
GridSearchCV
. - Set up a parameter grid for
"alpha"
, usingnp.linspace()
to create 20 evenly spaced values ranging from0.00001
to1
. - Call
GridSearchCV()
, passinglasso
, the parameter grid, and settingcv
equal tokf
. - 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_))