import pandas as pd
import numpy as np
import time
from sklearn.svm import SVC
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.datasets import load_breast_cancer

dataset = load_breast_cancer()
X, y = dataset.data, dataset.target

GridSearch#

from sklearn.model_selection import GridSearchCV

param_grid = {
    'C': [0.01, 0.1, 1, 10, 100, 1000],
    'degree': np.arange(0, 11)
}

grid_search = GridSearchCV(SVC(kernel='poly'), param_grid=param_grid, cv=3, verbose=2)
start_time = time.time()
grid_search.fit(X, y)
time_normal_gs = time.time() - start_time
Fitting 3 folds for each of 66 candidates, totalling 198 fits
[CV] END ...................................C=0.01, degree=0; total time=   0.0s
[CV] END ...................................C=0.01, degree=0; total time=   0.0s
[CV] END ...................................C=0.01, degree=0; total time=   0.0s
[CV] END ...................................C=0.01, degree=1; total time=   0.0s
[CV] END ...................................C=0.01, degree=1; total time=   0.0s
[CV] END ...................................C=0.01, degree=1; total time=   0.0s
[CV] END ...................................C=0.01, degree=2; total time=   0.0s
[CV] END ...................................C=0.01, degree=2; total time=   0.0s
[CV] END ...................................C=0.01, degree=2; total time=   0.0s
[CV] END ...................................C=0.01, degree=3; total time=   0.0s
[CV] END ...................................C=0.01, degree=3; total time=   0.0s
[CV] END ...................................C=0.01, degree=3; total time=   0.0s
[CV] END ...................................C=0.01, degree=4; total time=   0.0s
[CV] END ...................................C=0.01, degree=4; total time=   0.0s
[CV] END ...................................C=0.01, degree=4; total time=   0.0s
[CV] END ...................................C=0.01, degree=5; total time=   0.0s
[CV] END ...................................C=0.01, degree=5; total time=   0.0s
[CV] END ...................................C=0.01, degree=5; total time=   0.0s
[CV] END ...................................C=0.01, degree=6; total time=   0.0s
[CV] END ...................................C=0.01, degree=6; total time=   0.0s
[CV] END ...................................C=0.01, degree=6; total time=   0.0s
[CV] END ...................................C=0.01, degree=7; total time=   0.0s
[CV] END ...................................C=0.01, degree=7; total time=   0.0s
[CV] END ...................................C=0.01, degree=7; total time=   0.0s
[CV] END ...................................C=0.01, degree=8; total time=   0.0s
[CV] END ...................................C=0.01, degree=8; total time=   0.0s
[CV] END ...................................C=0.01, degree=8; total time=   0.0s
[CV] END ...................................C=0.01, degree=9; total time=   0.0s
[CV] END ...................................C=0.01, degree=9; total time=   0.0s
[CV] END ...................................C=0.01, degree=9; total time=   0.0s
[CV] END ..................................C=0.01, degree=10; total time=   0.0s
[CV] END ..................................C=0.01, degree=10; total time=   0.0s
[CV] END ..................................C=0.01, degree=10; total time=   0.0s
[CV] END ....................................C=0.1, degree=0; total time=   0.0s
[CV] END ....................................C=0.1, degree=0; total time=   0.0s
[CV] END ....................................C=0.1, degree=0; total time=   0.0s
[CV] END ....................................C=0.1, degree=1; total time=   0.0s
[CV] END ....................................C=0.1, degree=1; total time=   0.0s
[CV] END ....................................C=0.1, degree=1; total time=   0.0s
[CV] END ....................................C=0.1, degree=2; total time=   0.0s
[CV] END ....................................C=0.1, degree=2; total time=   0.0s
[CV] END ....................................C=0.1, degree=2; total time=   0.0s
[CV] END ....................................C=0.1, degree=3; total time=   0.0s
[CV] END ....................................C=0.1, degree=3; total time=   0.0s
[CV] END ....................................C=0.1, degree=3; total time=   0.0s
[CV] END ....................................C=0.1, degree=4; total time=   0.0s
[CV] END ....................................C=0.1, degree=4; total time=   0.0s
[CV] END ....................................C=0.1, degree=4; total time=   0.0s
[CV] END ....................................C=0.1, degree=5; total time=   0.0s
[CV] END ....................................C=0.1, degree=5; total time=   0.0s
[CV] END ....................................C=0.1, degree=5; total time=   0.0s
[CV] END ....................................C=0.1, degree=6; total time=   0.0s
[CV] END ....................................C=0.1, degree=6; total time=   0.0s
[CV] END ....................................C=0.1, degree=6; total time=   0.0s
[CV] END ....................................C=0.1, degree=7; total time=   0.0s
[CV] END ....................................C=0.1, degree=7; total time=   0.0s
[CV] END ....................................C=0.1, degree=7; total time=   0.0s
[CV] END ....................................C=0.1, degree=8; total time=   0.0s
[CV] END ....................................C=0.1, degree=8; total time=   0.0s
[CV] END ....................................C=0.1, degree=8; total time=   0.0s
[CV] END ....................................C=0.1, degree=9; total time=   0.0s
[CV] END ....................................C=0.1, degree=9; total time=   0.0s
[CV] END ....................................C=0.1, degree=9; total time=   0.0s
[CV] END ...................................C=0.1, degree=10; total time=   0.0s
[CV] END ...................................C=0.1, degree=10; total time=   0.0s
[CV] END ...................................C=0.1, degree=10; total time=   0.0s
[CV] END ......................................C=1, degree=0; total time=   0.0s
[CV] END ......................................C=1, degree=0; total time=   0.0s
[CV] END ......................................C=1, degree=0; total time=   0.0s
[CV] END ......................................C=1, degree=1; total time=   0.0s
[CV] END ......................................C=1, degree=1; total time=   0.0s
[CV] END ......................................C=1, degree=1; total time=   0.0s
[CV] END ......................................C=1, degree=2; total time=   0.0s
[CV] END ......................................C=1, degree=2; total time=   0.0s
[CV] END ......................................C=1, degree=2; total time=   0.0s
[CV] END ......................................C=1, degree=3; total time=   0.0s
[CV] END ......................................C=1, degree=3; total time=   0.0s
[CV] END ......................................C=1, degree=3; total time=   0.0s
[CV] END ......................................C=1, degree=4; total time=   0.0s
[CV] END ......................................C=1, degree=4; total time=   0.0s
[CV] END ......................................C=1, degree=4; total time=   0.0s
[CV] END ......................................C=1, degree=5; total time=   0.0s
[CV] END ......................................C=1, degree=5; total time=   0.0s
[CV] END ......................................C=1, degree=5; total time=   0.0s
[CV] END ......................................C=1, degree=6; total time=   0.0s
[CV] END ......................................C=1, degree=6; total time=   0.0s
[CV] END ......................................C=1, degree=6; total time=   0.0s
[CV] END ......................................C=1, degree=7; total time=   0.0s
[CV] END ......................................C=1, degree=7; total time=   0.0s
[CV] END ......................................C=1, degree=7; total time=   0.0s
[CV] END ......................................C=1, degree=8; total time=   0.0s
[CV] END ......................................C=1, degree=8; total time=   0.0s
[CV] END ......................................C=1, degree=8; total time=   0.0s
[CV] END ......................................C=1, degree=9; total time=   0.0s
[CV] END ......................................C=1, degree=9; total time=   0.0s
[CV] END ......................................C=1, degree=9; total time=   0.0s
[CV] END .....................................C=1, degree=10; total time=   0.0s
[CV] END .....................................C=1, degree=10; total time=   0.0s
[CV] END .....................................C=1, degree=10; total time=   0.0s
[CV] END .....................................C=10, degree=0; total time=   0.0s
[CV] END .....................................C=10, degree=0; total time=   0.0s
[CV] END .....................................C=10, degree=0; total time=   0.0s
[CV] END .....................................C=10, degree=1; total time=   0.0s
[CV] END .....................................C=10, degree=1; total time=   0.0s
[CV] END .....................................C=10, degree=1; total time=   0.0s
[CV] END .....................................C=10, degree=2; total time=   0.0s
[CV] END .....................................C=10, degree=2; total time=   0.0s
[CV] END .....................................C=10, degree=2; total time=   0.0s
[CV] END .....................................C=10, degree=3; total time=   0.0s
[CV] END .....................................C=10, degree=3; total time=   0.0s
[CV] END .....................................C=10, degree=3; total time=   0.0s
[CV] END .....................................C=10, degree=4; total time=   0.0s
[CV] END .....................................C=10, degree=4; total time=   0.0s
[CV] END .....................................C=10, degree=4; total time=   0.0s
[CV] END .....................................C=10, degree=5; total time=   0.0s
[CV] END .....................................C=10, degree=5; total time=   0.0s
[CV] END .....................................C=10, degree=5; total time=   0.0s
[CV] END .....................................C=10, degree=6; total time=   0.0s
[CV] END .....................................C=10, degree=6; total time=   0.0s
[CV] END .....................................C=10, degree=6; total time=   0.0s
[CV] END .....................................C=10, degree=7; total time=   0.0s
[CV] END .....................................C=10, degree=7; total time=   0.0s
[CV] END .....................................C=10, degree=7; total time=   0.0s
[CV] END .....................................C=10, degree=8; total time=   0.0s
[CV] END .....................................C=10, degree=8; total time=   0.0s
[CV] END .....................................C=10, degree=8; total time=   0.0s
[CV] END .....................................C=10, degree=9; total time=   0.0s
[CV] END .....................................C=10, degree=9; total time=   0.0s
[CV] END .....................................C=10, degree=9; total time=   0.0s
[CV] END ....................................C=10, degree=10; total time=   0.0s
[CV] END ....................................C=10, degree=10; total time=   0.0s
[CV] END ....................................C=10, degree=10; total time=   0.0s
[CV] END ....................................C=100, degree=0; total time=   0.0s
[CV] END ....................................C=100, degree=0; total time=   0.0s
[CV] END ....................................C=100, degree=0; total time=   0.0s
[CV] END ....................................C=100, degree=1; total time=   0.0s
[CV] END ....................................C=100, degree=1; total time=   0.0s
[CV] END ....................................C=100, degree=1; total time=   0.0s
[CV] END ....................................C=100, degree=2; total time=   0.0s
[CV] END ....................................C=100, degree=2; total time=   0.0s
[CV] END ....................................C=100, degree=2; total time=   0.0s
[CV] END ....................................C=100, degree=3; total time=   0.0s
[CV] END ....................................C=100, degree=3; total time=   0.0s
[CV] END ....................................C=100, degree=3; total time=   0.0s
[CV] END ....................................C=100, degree=4; total time=   0.0s
[CV] END ....................................C=100, degree=4; total time=   0.0s
[CV] END ....................................C=100, degree=4; total time=   0.0s
[CV] END ....................................C=100, degree=5; total time=   0.0s
[CV] END ....................................C=100, degree=5; total time=   0.0s
[CV] END ....................................C=100, degree=5; total time=   0.0s
[CV] END ....................................C=100, degree=6; total time=   0.0s
[CV] END ....................................C=100, degree=6; total time=   0.0s
[CV] END ....................................C=100, degree=6; total time=   0.0s
[CV] END ....................................C=100, degree=7; total time=   0.0s
[CV] END ....................................C=100, degree=7; total time=   0.0s
[CV] END ....................................C=100, degree=7; total time=   0.0s
[CV] END ....................................C=100, degree=8; total time=   0.0s
[CV] END ....................................C=100, degree=8; total time=   0.0s
[CV] END ....................................C=100, degree=8; total time=   0.0s
[CV] END ....................................C=100, degree=9; total time=   0.0s
[CV] END ....................................C=100, degree=9; total time=   0.0s
[CV] END ....................................C=100, degree=9; total time=   0.0s
[CV] END ...................................C=100, degree=10; total time=   0.0s
[CV] END ...................................C=100, degree=10; total time=   0.0s
[CV] END ...................................C=100, degree=10; total time=   0.0s
[CV] END ...................................C=1000, degree=0; total time=   0.0s
[CV] END ...................................C=1000, degree=0; total time=   0.0s
[CV] END ...................................C=1000, degree=0; total time=   0.0s
[CV] END ...................................C=1000, degree=1; total time=   0.0s
[CV] END ...................................C=1000, degree=1; total time=   0.0s
[CV] END ...................................C=1000, degree=1; total time=   0.0s
[CV] END ...................................C=1000, degree=2; total time=   0.0s
[CV] END ...................................C=1000, degree=2; total time=   0.0s
[CV] END ...................................C=1000, degree=2; total time=   0.0s
[CV] END ...................................C=1000, degree=3; total time=   0.0s
[CV] END ...................................C=1000, degree=3; total time=   0.0s
[CV] END ...................................C=1000, degree=3; total time=   0.0s
[CV] END ...................................C=1000, degree=4; total time=   0.0s
[CV] END ...................................C=1000, degree=4; total time=   0.0s
[CV] END ...................................C=1000, degree=4; total time=   0.0s
[CV] END ...................................C=1000, degree=5; total time=   0.0s
[CV] END ...................................C=1000, degree=5; total time=   0.0s
[CV] END ...................................C=1000, degree=5; total time=   0.0s
[CV] END ...................................C=1000, degree=6; total time=   0.0s
[CV] END ...................................C=1000, degree=6; total time=   0.0s
[CV] END ...................................C=1000, degree=6; total time=   0.0s
[CV] END ...................................C=1000, degree=7; total time=   0.0s
[CV] END ...................................C=1000, degree=7; total time=   0.0s
[CV] END ...................................C=1000, degree=7; total time=   0.0s
[CV] END ...................................C=1000, degree=8; total time=   0.0s
[CV] END ...................................C=1000, degree=8; total time=   0.0s
[CV] END ...................................C=1000, degree=8; total time=   0.0s
[CV] END ...................................C=1000, degree=9; total time=   0.0s
[CV] END ...................................C=1000, degree=9; total time=   0.0s
[CV] END ...................................C=1000, degree=9; total time=   0.0s
[CV] END ..................................C=1000, degree=10; total time=   0.0s
[CV] END ..................................C=1000, degree=10; total time=   0.0s
[CV] END ..................................C=1000, degree=10; total time=   0.0s
grid_search.best_score_
0.9367399981435068
grid_search.best_params_
{'C': 1000, 'degree': 5}
gs_results = pd.DataFrame(grid_search.cv_results_)
gs_results.sort_values(by='mean_test_score', ascending=False, inplace=True)
gs_results.head()
mean_fit_time std_fit_time mean_score_time std_score_time param_C param_degree params split0_test_score split1_test_score split2_test_score mean_test_score std_test_score rank_test_score
60 0.006015 0.001436 0.000654 0.000463 1000.0 5 {'C': 1000, 'degree': 5} 0.905263 0.963158 0.941799 0.936740 0.023905 1
57 0.004333 0.000943 0.000671 0.000474 1000.0 2 {'C': 1000, 'degree': 2} 0.915789 0.963158 0.931217 0.936721 0.019726 2
61 0.010333 0.006204 0.001010 0.000012 1000.0 6 {'C': 1000, 'degree': 6} 0.905263 0.957895 0.941799 0.934986 0.022020 3
59 0.005172 0.001309 0.000339 0.000480 1000.0 4 {'C': 1000, 'degree': 4} 0.905263 0.963158 0.936508 0.934976 0.023660 4
56 0.002331 0.000473 0.000667 0.000472 1000.0 1 {'C': 1000, 'degree': 1} 0.910526 0.968421 0.925926 0.934958 0.024483 5
from sklearn.metrics import ConfusionMatrixDisplay

ConfusionMatrixDisplay.from_estimator(grid_search, X, y)
<sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at 0x228a2921400>
../../../_images/40a4db64aed8f88ae83e14a6e4c6a7e860c90b0591edfe5188b8a6c9a2831b89.png