9951 explained code solutions for 126 technologies


python-scikit-learnHow to get best estimator from grid search CV


from sklearn import svm, datasets, model_selection

iris = datasets.load_iris()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}

clf = model_selection.GridSearchCV(svm.SVC(), parameters)
clf.fit(iris.data, iris.target)

estimator = clf.best_estimator_ctrl + c
from sklearn import

import module from scikit-learn

load_iris

loads Iris dataset

parameters

parameters dictionary to run grid search accross

.GridSearchCV(

creates GridSearchCV model

svm.SVC()

use SVC model as an estimator

.fit(

train transformation model

.best_estimator_

returns estimator which gave highest score


Usage example

from sklearn import svm, datasets, model_selection

iris = datasets.load_iris()
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}

clf = model_selection.GridSearchCV(svm.SVC(), parameters)
clf.fit(iris.data, iris.target)

print(clf.best_estimator_)
output
SVC(C=1, kernel='linear')