9951 explained code solutions for 126 technologies


python-scikit-learnHow to get best parameters 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)

params = clf.best_params_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_params_

dictionary with best parameters names and values


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_params_)
output
{'C': 1, 'kernel': 'linear'}