# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:ptlesson] *
#     language: python
#     name: conda-env-ptlesson-py
# ---

# +
# 모형 최적화: 파라미터 튜닝
# ref: https://datascienceschool.net/view-notebook/ff4b5d491cc34f94aea04baca86fbef8/

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import make_classification
from matplotlib import rc
import matplotlib as mpl
import warnings
warnings.filterwarnings(action="ignore")
rc('font', family="AppleGothic")
# %matplotlib inline

### Validation Curve: 성능 기준 세팅
from sklearn.datasets import load_digits
from sklearn.svm import SVC
from sklearn.model_selection import validation_curve

digits= load_digits()
X, y = digits.data, digits.target

param_range = np.logspace(-6, -1, 10)
# -

# %%time
train_scores, test_scores = validation_curve(
                                SVC(), X, y, 
                                param_name="gamma", 
                                param_range=param_range,
                                cv=10, 
                                scoring="accuracy",
                                n_jobs=1  # number of jobs to run in parallel: -1 means using all processors
)

# +
train_scores_mean = np.mean(train_scores, axis=1)
train_scores_std  = np.std(train_scores, axis=1)
test_scores_mean  = np.mean(test_scores, axis=1)
test_scores_std   = np.std(test_scores, axis=1)

mpl.rcParams['font.family'] = 'DejaVu Sans'
plt.semilogx(param_range, train_scores_mean, label="Training score", color="r")
plt.fill_between(param_range, 
                 train_scores_mean - train_scores_std, 
                 train_scores_mean + train_scores_std, alpha=0.2, color="r")
plt.semilogx(param_range, test_scores_mean, label="Cross-validation score", color="g")
plt.fill_between(param_range, 
                 test_scores_mean - test_scores_std, 
                 test_scores_mean + test_scores_std, alpha=0.2, color="g")
plt.legend(loc="best")
plt.xlabel("$\gamma$")
plt.ylabel("Score")
plt.title("Validation Curve w/ SVM")
plt.show()

# +
### Grid Search 
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler # substracts mean, unit variant
from sklearn.svm import SVC

pipe_svc = Pipeline([('scl', StandardScaler()), ('clf', SVC(random_state=1))])
param_range = [ np.exp(-4+i) for i in range(8) ]
param_grid = [
    {'clf__C' : param_range, 'clf__kernel': ['linear']},
    {'clf__C' : param_range, 'clf__gamma': param_range, 'clf__kernel':['rbf']}
]
gs = GridSearchCV(estimator=pipe_svc, param_grid=param_grid,
                  scoring='accuracy', cv=10, n_jobs=1)
# %time gs = gs.fit(X, y)
# -

gs.cv_results_["params"]

gs.cv_results_["mean_test_score"]

print(gs.best_score_)
print(gs.best_params_)

# +
### Parameter Grid: 임의의 파라미터 조합하여 탐색할 경우
from sklearn.model_selection import ParameterGrid

# for example,
param_grid = {'a':[1,2], 'b': [True, False]}
list(ParameterGrid(param_grid))
# -

param_grid = [{'kernel': ['linear']}, {'kernel': ['rbf'], 'gamma': [1,10]}]
list(ParameterGrid(param_grid))

# +
### 병렬 처리: GridSearchCV나 validation_curve의 n_jobs 값을 늘려주면 parallel processor 사용 가능
# -



StarcoderdataJupyterScriptsDedupFiltered