from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target,
random_state=0)
param_grid = [{'kernel': ['rbf'],
'C': [0.001, 0.01, 0.1, 1, 10, 100],
'gamma': [0.001, 0.01, 0.1, 1, 10, 100]},
{'kernel': ['linear'],
'C': [0.001, 0.01, 0.1, 1, 10, 100]}]
print("그리드 목록:\n{}".format(param_grid))
그리드 목록: [{'kernel': ['rbf'], 'C': [0.001, 0.01, 0.1, 1, 10, 100], 'gamma': [0.001, 0.01, 0.1, 1, 10, 100]}, {'kernel': ['linear'], 'C': [0.001, 0.01, 0.1, 1, 10, 100]}]
grid_search = GridSearchCV(SVC(), param_grid, cv=5, return_train_score=True)
grid_search.fit(X_train, y_train)
print("최적 파라미터: {}".format(grid_search.best_params_))
print("최고 교차 검증 점수: {:.2f}".format(grid_search.best_score_))
최적 파라미터: {'C': 10, 'gamma': 0.1, 'kernel': 'rbf'} 최고 교차 검증 점수: 0.97
results = pd.DataFrame(grid_search.cv_results_)
# 좀 더 나은 출력을 위해 결과를 전치시킵니다
display(results.T)
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
mean_fit_time | 0.00418963 | 0.0013916 | 0.00121255 | 0.00100226 | 0.00101514 | 0.000199699 | 0 | 0.000227833 | 0.00166535 | 0.00161576 | ... | 0 | 0 | 0.00205283 | 0.00163336 | 0.00204363 | 0.000409651 | 0.000417042 | 0.00162253 | 0.000426292 | 0 |
std_fit_time | 0.00158592 | 0.000483649 | 0.00041019 | 1.7953e-05 | 1.98375e-05 | 0.000399399 | 0 | 0.000455666 | 0.00190458 | 0.00323153 | ... | 0 | 0 | 0.00315121 | 0.00326672 | 0.0031262 | 0.000819302 | 0.000834084 | 0.00324507 | 0.000852585 | 0 |
mean_score_time | 0.00158443 | 0.000401497 | 0.000804567 | 0.000998831 | 0.000390482 | 0 | 0 | 0.003125 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.00042491 | 0 | 0.001612 |
std_score_time | 0.000802272 | 0.000491736 | 0.000403458 | 1.33685e-06 | 0.000478431 | 0 | 0 | 0.00625 | 0 | 0 | ... | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0.000849819 | 0 | 0.00322399 |
param_C | 0.001 | 0.001 | 0.001 | 0.001 | 0.001 | 0.001 | 0.01 | 0.01 | 0.01 | 0.01 | ... | 100 | 100 | 100 | 100 | 0.001 | 0.01 | 0.1 | 1 | 10 | 100 |
param_gamma | 0.001 | 0.01 | 0.1 | 1 | 10 | 100 | 0.001 | 0.01 | 0.1 | 1 | ... | 0.1 | 1 | 10 | 100 | NaN | NaN | NaN | NaN | NaN | NaN |
param_kernel | rbf | rbf | rbf | rbf | rbf | rbf | rbf | rbf | rbf | rbf | ... | rbf | rbf | rbf | rbf | linear | linear | linear | linear | linear | linear |
params | {'C': 0.001, 'gamma': 0.001, 'kernel': 'rbf'} | {'C': 0.001, 'gamma': 0.01, 'kernel': 'rbf'} | {'C': 0.001, 'gamma': 0.1, 'kernel': 'rbf'} | {'C': 0.001, 'gamma': 1, 'kernel': 'rbf'} | {'C': 0.001, 'gamma': 10, 'kernel': 'rbf'} | {'C': 0.001, 'gamma': 100, 'kernel': 'rbf'} | {'C': 0.01, 'gamma': 0.001, 'kernel': 'rbf'} | {'C': 0.01, 'gamma': 0.01, 'kernel': 'rbf'} | {'C': 0.01, 'gamma': 0.1, 'kernel': 'rbf'} | {'C': 0.01, 'gamma': 1, 'kernel': 'rbf'} | ... | {'C': 100, 'gamma': 0.1, 'kernel': 'rbf'} | {'C': 100, 'gamma': 1, 'kernel': 'rbf'} | {'C': 100, 'gamma': 10, 'kernel': 'rbf'} | {'C': 100, 'gamma': 100, 'kernel': 'rbf'} | {'C': 0.001, 'kernel': 'linear'} | {'C': 0.01, 'kernel': 'linear'} | {'C': 0.1, 'kernel': 'linear'} | {'C': 1, 'kernel': 'linear'} | {'C': 10, 'kernel': 'linear'} | {'C': 100, 'kernel': 'linear'} |
split0_test_score | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | ... | 1 | 0.956522 | 0.869565 | 0.521739 | 0.347826 | 0.869565 | 1 | 1 | 1 | 0.956522 |
split1_test_score | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | 0.347826 | ... | 0.956522 | 0.956522 | 0.913043 | 0.521739 | 0.347826 | 0.869565 | 0.913043 | 0.956522 | 1 | 0.956522 |
split2_test_score | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | ... | 1 | 1 | 1 | 0.590909 | 0.363636 | 0.772727 | 1 | 1 | 1 | 1 |
split3_test_score | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | 0.363636 | ... | 0.863636 | 0.863636 | 0.818182 | 0.590909 | 0.363636 | 0.772727 | 0.909091 | 0.954545 | 0.909091 | 0.909091 |
split4_test_score | 0.409091 | 0.409091 | 0.409091 | 0.409091 | 0.409091 | 0.409091 | 0.409091 | 0.409091 | 0.409091 | 0.409091 | ... | 0.954545 | 0.954545 | 0.954545 | 0.681818 | 0.409091 | 0.909091 | 0.954545 | 0.954545 | 0.954545 | 0.954545 |
mean_test_score | 0.366403 | 0.366403 | 0.366403 | 0.366403 | 0.366403 | 0.366403 | 0.366403 | 0.366403 | 0.366403 | 0.366403 | ... | 0.954941 | 0.946245 | 0.911067 | 0.581423 | 0.366403 | 0.838735 | 0.955336 | 0.973123 | 0.972727 | 0.955336 |
std_test_score | 0.0224845 | 0.0224845 | 0.0224845 | 0.0224845 | 0.0224845 | 0.0224845 | 0.0224845 | 0.0224845 | 0.0224845 | 0.0224845 | ... | 0.0497992 | 0.0447077 | 0.0634877 | 0.0589635 | 0.0224845 | 0.0557943 | 0.0397936 | 0.0219572 | 0.0363636 | 0.0287643 |
rank_test_score | 27 | 27 | 27 | 27 | 27 | 27 | 27 | 27 | 27 | 27 | ... | 11 | 13 | 18 | 24 | 27 | 21 | 8 | 1 | 3 | 8 |
split0_train_score | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | ... | 0.988764 | 1 | 1 | 1 | 0.370787 | 0.853933 | 0.966292 | 0.988764 | 0.988764 | 0.977528 |
split1_train_score | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | 0.370787 | ... | 0.988764 | 1 | 1 | 1 | 0.370787 | 0.910112 | 0.966292 | 0.977528 | 0.988764 | 0.988764 |
split2_train_score | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | ... | 0.977778 | 1 | 1 | 1 | 0.366667 | 0.844444 | 0.944444 | 0.977778 | 0.977778 | 0.988889 |
split3_train_score | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | 0.366667 | ... | 1 | 1 | 1 | 1 | 0.366667 | 0.766667 | 0.966667 | 0.988889 | 0.988889 | 1 |
split4_train_score | 0.355556 | 0.355556 | 0.355556 | 0.355556 | 0.355556 | 0.355556 | 0.355556 | 0.355556 | 0.355556 | 0.355556 | ... | 1 | 1 | 1 | 1 | 0.355556 | 0.877778 | 0.966667 | 0.988889 | 1 | 1 |
mean_train_score | 0.366092 | 0.366092 | 0.366092 | 0.366092 | 0.366092 | 0.366092 | 0.366092 | 0.366092 | 0.366092 | 0.366092 | ... | 0.991061 | 1 | 1 | 1 | 0.366092 | 0.850587 | 0.962072 | 0.98437 | 0.988839 | 0.991036 |
std_train_score | 0.00558129 | 0.00558129 | 0.00558129 | 0.00558129 | 0.00558129 | 0.00558129 | 0.00558129 | 0.00558129 | 0.00558129 | 0.00558129 | ... | 0.00832835 | 0 | 0 | 0 | 0.00558129 | 0.0476952 | 0.00881557 | 0.00548484 | 0.00702755 | 0.00840169 |
23 rows × 42 columns
from sklearn.model_selection import cross_val_score
param_grid = { 'C' : [0.001, 0.01, 0.1, 1, 10, 100],
'gamma': [0.001, 0.01, 0.1, 1, 10, 100] }
scores = cross_val_score(GridSearchCV(SVC(), param_grid, cv=5),
iris.data, iris.target, cv=5)
print("교차 검증 점수: ", scores)
print("교차 검증 평균 점수: ", scores.mean())
print(param_grid)
교차 검증 점수: [0.96666667 1. 0.96666667 0.96666667 1. ] 교차 검증 평균 점수: 0.9800000000000001 {'C': [0.001, 0.01, 0.1, 1, 10, 100], 'gamma': [0.001, 0.01, 0.1, 1, 10, 100]}