9951 explained code solutions for 126 technologies


python-scikit-learnGrid search CV 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)

results = clf.cv_results_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

.cv_results_

dictionary with results after model training and parameters search


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.cv_results_['params'])
output
[{'C': 1, 'kernel': 'linear'}, {'C': 1, 'kernel': 'rbf'}, {'C': 10, 'kernel': 'linear'}, {'C': 10, 'kernel': 'rbf'}]