<jupyter_start><jupyter_text>Experimento 10: _grid search_ e validação com SVM usando FS13 e FS20 Importando bibliotecas<jupyter_code>import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

from sklearn.model_selection import GridSearchCV, GroupKFold
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVC
# Atributos FS13
FS13 = ['kurtosis-A3', 'meanAD-A3',
        'medianAD-A3', 'medianAD-D3',
        'energy-A3', 'energy-D3']

def col_in_fs13(string:str) -> bool:
    for feature in FS13:
        if feature in string:
            return True
    return False


chb_df = pd.read_csv('./chb.csv')

fs13_columns = np.array([col for col in chb_df.columns if col_in_fs13(col)])

groups = chb_df.pop('chb')
y = chb_df.pop('target').values
X = chb_df[fs13_columns].values
results = GridSearchCV(
    estimator=make_pipeline(StandardScaler(), SVC()),
    param_grid={'svc__C':[0.1, 1, 10, 100, 1000, 10_000],
                'svc__gamma':np.arange(0.5, 1.5, 0.1)},
    cv=GroupKFold(n_splits=24),
    ).fit(
        X=X,
        y=y,
        groups=groups
        )

results_df = pd.DataFrame(results.cv_results_)
results_df
sns.pointplot(
    data=results_df,
    x='param_svc__C',
    y='mean_test_score',
    hue='param_svc__gamma',
    ).set(
        xlabel='Parâmetro C',
        ylabel='Acurácia média')

plt.legend(bbox_to_anchor=(1, 1))

results_df[[
    'param_svc__C',
    'param_svc__gamma',
    'mean_test_score',
    'std_test_score',
    'rank_test_score'
    ]].style.hide_index().background_gradient(cmap='Blues_r', subset=['rank_test_score'])
acc_list = []

best_C = results.best_params_['svc__C']
best_gamma = results.best_params_['svc__gamma']

pipe = make_pipeline(StandardScaler(),
                     SVC(C=best_C, gamma=best_gamma, cache_size=500))

for train_index, test_index in GroupKFold(n_splits=24).split(X, y, groups):
    # Separando dados
    y_train, y_test = y[train_index], y[test_index]
    X_train, X_test = X[train_index], X[test_index]

    acc_list.append(
        pipe.fit(X_train, y_train).score(X_test, y_test)
    )


svm_df = pd.read_csv('./04-fs20-svm.csv', index_col=0)
svm_df['fs13'] = acc_list

svm_df.boxplot()

print(svm_df.describe())

svm_df.style.hide_index().background_gradient(cmap='Blues')<jupyter_output>fs20       6_ch       fs13
count  24.000000  24.000000  24.000000
mean    0.522252   0.789761   0.664857
std     0.044117   0.180005   0.155951
min     0.447674   0.404412   0.329457
25%     0.497768   0.789683   0.578977
50%     0.517276   0.852780   0.666928
75%     0.545861   0.898153   0.766039
max     0.622449   0.979798   0.905455
{'fraction_non_alphanumeric': 0.1240768094534712, 'fraction_numerical': 0.09121122599704579, 'mean_word_length': 3.477685950413223, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '520472', 'license': 'MIT', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1078, 'n_words': 202, 'path': 'ML/f02-chb/feature_set/05-fs13-gs_svm_c-gamma.ipynb', 'repo_name': 'newtmagalhaes/Machine-Learning', 'size': '2708'}