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_))
This exercise is part of the course
Supervised Learning with scikit-learn
Grow your machine learning skills with scikit-learn in Python. Use real-world datasets in this interactive course and learn how to make powerful predictions!
Having trained models, now you will learn how to evaluate them. In this chapter, you will be introduced to several metrics along with a visualization technique for analyzing classification model performance using scikit-learn. You will also learn how to optimize classification and regression models through the use of hyperparameter tuning.
Exercise 1: How good is your model?Exercise 2: Deciding on a primary metricExercise 3: Assessing a diabetes prediction classifierExercise 4: Logistic regression and the ROC curveExercise 5: Building a logistic regression modelExercise 6: The ROC curveExercise 7: ROC AUCExercise 8: Hyperparameter tuningExercise 9: Hyperparameter tuning with GridSearchCVExercise 10: Hyperparameter tuning with RandomizedSearchCVWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.