5.2. Grid Search¶
Grid Search is used to optimize the parameters of a model (e.g. Support Vector Classifier, Lasso, etc.) using cross-validation.
Main class is GridSearchCV.
5.2.1. Examples¶
See Parameter estimation using grid search with a nested cross-validation for an example of Grid Search computation on the digits dataset.
See Sample pipeline for text feature extraction and evaluation for an example of Grid Search coupling parameters from a text documents feature extractor (n-gram count vectorizer and TF-IDF transformer) with a classifier (here a linear SVM trained with SGD with either elastic net or L2 penalty).
Note
Computations can be run in parallel if your OS supports it, by using the keyword n_jobs=-1, see function signature for more details.
5.2.2. Alternatives to brute force grid search¶
5.2.2.1. Model specific cross-validation¶
Some models can fit data for a range of value of some parameter almost as efficiently as fitting the estimator for a single value of the parameter. This feature can be leveraged to perform a more efficient cross-validation used for model selection of this parameter.
The most common parameter amenable to this strategy is the parameter encoding the strength of the regularizer. In this case we say that we compute the regularization path of the estimator.
Here is the list of such models:
linear_model.RidgeCV([alphas, ...]) | Ridge regression with built-in cross-validation. |
linear_model.RidgeClassifierCV([alphas, ...]) | |
linear_model.LarsCV([fit_intercept, ...]) | Cross-validated Least Angle Regression model |
linear_model.LassoLarsCV([fit_intercept, ...]) | Cross-validated Lasso, using the LARS algorithm |
linear_model.LassoCV([eps, n_alphas, ...]) | Lasso linear model with iterative fitting along a regularization path |
linear_model.ElasticNetCV([rho, eps, ...]) | Elastic Net model with iterative fitting along a regularization path |
5.2.2.2. Information Criterion¶
Some models can offer an information-theoretic closed-form formula of the optimal estimate of the regularization parameter by computing a single regularization path (instead of several when using cross-validation).
Here is the list of models benefitting from the Aikike Information Criterion (AIC) or the Bayesian Information Criterion (BIC) for automated model selection:
linear_model.LassoLarsIC([criterion, ...]) | Lasso model fit with Lars using BIC or AIC for model selection |