class sklearn.model_selection.GridSearchCV(estimator, param_grid, scoring=None, fit_params=None, n_jobs=None, iid=’warn’, refit=True, cv=’warn’, verbose=0, pre_dispatch=‘2*n_jobs’, error_score=’raise-deprecating’, return_train_score=’warn’) [source]
Exhaustive search over specified parameter values for an estimator.
Important members are fit, predict.
GridSearchCV implements a “fit” and a “score” method. It also implements “predict”, “predict_proba”, “decision_function”, “transform” and “inverse_transform” if they are implemented in the estimator used.
The parameters of the estimator used to apply these methods are optimized by cross-validated grid-search over a parameter grid.
Read more in the User Guide.
| Parameters: |
| ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Attributes: |
|
See also
ParameterGrid
sklearn.model_selection.train_test_split
sklearn.metrics.make_scorer
The parameters selected are those that maximize the score of the left out data, unless an explicit score is passed in which case it is used instead.
If n_jobs was set to a value higher than one, the data is copied for each point in the grid (and not n_jobs times). This is done for efficiency reasons if individual jobs take very little time, but may raise errors if the dataset is large and not enough memory is available. A workaround in this case is to set pre_dispatch. Then, the memory is copied only pre_dispatch many times. A reasonable value for pre_dispatch is 2 * n_jobs.
>>> from sklearn import svm, datasets
>>> from sklearn.model_selection import GridSearchCV
>>> iris = datasets.load_iris()
>>> parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
>>> svc = svm.SVC(gamma="scale")
>>> clf = GridSearchCV(svc, parameters, cv=5)
>>> clf.fit(iris.data, iris.target)
...
GridSearchCV(cv=5, error_score=...,
estimator=SVC(C=1.0, cache_size=..., class_weight=..., coef0=...,
decision_function_shape='ovr', degree=..., gamma=...,
kernel='rbf', max_iter=-1, probability=False,
random_state=None, shrinking=True, tol=...,
verbose=False),
fit_params=None, iid=..., n_jobs=None,
param_grid=..., pre_dispatch=..., refit=..., return_train_score=...,
scoring=..., verbose=...)
>>> sorted(clf.cv_results_.keys())
...
['mean_fit_time', 'mean_score_time', 'mean_test_score',...
'mean_train_score', 'param_C', 'param_kernel', 'params',...
'rank_test_score', 'split0_test_score',...
'split0_train_score', 'split1_test_score', 'split1_train_score',...
'split2_test_score', 'split2_train_score',...
'std_fit_time', 'std_score_time', 'std_test_score', 'std_train_score'...]
decision_function(X) | Call decision_function on the estimator with the best found parameters. |
fit(X[, y, groups]) | Run fit with all sets of parameters. |
get_params([deep]) | Get parameters for this estimator. |
inverse_transform(Xt) | Call inverse_transform on the estimator with the best found params. |
predict(X) | Call predict on the estimator with the best found parameters. |
predict_log_proba(X) | Call predict_log_proba on the estimator with the best found parameters. |
predict_proba(X) | Call predict_proba on the estimator with the best found parameters. |
score(X[, y]) | Returns the score on the given data, if the estimator has been refit. |
set_params(**params) | Set the parameters of this estimator. |
transform(X) | Call transform on the estimator with the best found parameters. |
__init__(estimator, param_grid, scoring=None, fit_params=None, n_jobs=None, iid=’warn’, refit=True, cv=’warn’, verbose=0, pre_dispatch=‘2*n_jobs’, error_score=’raise-deprecating’, return_train_score=’warn’) [source]
decision_function(X) [source]
Call decision_function on the estimator with the best found parameters.
Only available if refit=True and the underlying estimator supports decision_function.
| Parameters: |
|
|---|
fit(X, y=None, groups=None, **fit_params) [source]
Run fit with all sets of parameters.
| Parameters: |
|
|---|
get_params(deep=True) [source]
Get parameters for this estimator.
| Parameters: |
|
|---|---|
| Returns: |
|
inverse_transform(Xt) [source]
Call inverse_transform on the estimator with the best found params.
Only available if the underlying estimator implements inverse_transform and refit=True.
| Parameters: |
|
|---|
predict(X) [source]
Call predict on the estimator with the best found parameters.
Only available if refit=True and the underlying estimator supports predict.
| Parameters: |
|
|---|
predict_log_proba(X) [source]
Call predict_log_proba on the estimator with the best found parameters.
Only available if refit=True and the underlying estimator supports predict_log_proba.
| Parameters: |
|
|---|
predict_proba(X) [source]
Call predict_proba on the estimator with the best found parameters.
Only available if refit=True and the underlying estimator supports predict_proba.
| Parameters: |
|
|---|
score(X, y=None) [source]
Returns the score on the given data, if the estimator has been refit.
This uses the score defined by scoring where provided, and the best_estimator_.score method otherwise.
| Parameters: |
|
|---|---|
| Returns: |
|
set_params(**params) [source]
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.
| Returns: |
|
|---|
transform(X) [source]
Call transform on the estimator with the best found parameters.
Only available if the underlying estimator supports transform and refit=True.
| Parameters: |
|
|---|
sklearn.model_selection.GridSearchCV
© 2007–2018 The scikit-learn developers
Licensed under the 3-clause BSD License.
http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html