text_plain_output_1.png
from sklearn import svm, datasets

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import cross_val_score

import numpy as np

import pandas as pd

from sklearn import svm, datasets
iris = datasets.load_iris()

df = pd.DataFrame(iris.data, columns=iris.feature_names)

df['target'] = iris.target

df['target'] = df['target'].apply(lambda x: iris.target_names[x])

result = cross_val_score(svm.SVC(kernel='linear', C=10, gamma='auto'), iris.data, iris.target, cv=5)
np.mean(result)

result = cross_val_score(svm.SVC(kernel='rbf', C=5, gamma='auto'), iris.data, iris.target, cv=5)
np.mean(result)

kernels = ['rbf', 'linear']
C = [1, 10, 20, 30]
avg_scores = {}
for kval in kernels:
    for cval in C:
        cv_scores = cross_val_score(svm.SVC(kernel=kval, C=cval), iris.data, iris.target, cv=5)
        avg_scores[kval + '_' + str(cval)] = np.mean(cv_scores)
avg_scores

from sklearn.model_selection import GridSearchCV
clf = GridSearchCV(svm.SVC(gamma='auto'), {'C': [1, 5, 10, 7, 3, 12], 'kernel': ['rbf', 'linear', 'sigmoid', 'poly'], 'max_iter': [1, 3, 5, 7, 9, 12, 25, 50, 100]}, cv=10)
clf.fit(iris.data, iris.target)
clf.cv_results_

df = pd.DataFrame(clf.cv_results_)
df

clf.best_score_

clf.best_params_