<jupyter_start><jupyter_text>Prática Supervisionada<jupyter_code>import pandas as pd
from sklearn import model_selection
from sklearn import preprocessing<jupyter_output><empty_output><jupyter_text>Carregue o dataset wine.data<jupyter_code>df = pd.read_csv('wine.data', header=None)
df[0].unique()



df.head()<jupyter_output><empty_output><jupyter_text>Dividir os dados entre treino e teste de forma estratificada<jupyter_code>df.isnull().sum()



df.info()
X = df.values[:, 1:]
y = df.values[:, 0]
X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y,
                            stratify=y, test_size=0.30, random_state=42) 
X_train.shape, X_test.shape, y_train.shape, y_test.shape<jupyter_output><empty_output><jupyter_text>Provar a estratificação dos dados com relação aos labels<jupyter_code>import numpy as np
count_y_train = pd.Series(y_train).value_counts()
count_y_test = pd.Series(y_test).value_counts()


print(df[0].value_counts()/np.sum(df[0].value_counts()))
print(count_y_train/np.sum(count_y_train))
print(count_y_test/np.sum(count_y_test))<jupyter_output>2    0.398876
1    0.331461
3    0.269663
Name: 0, dtype: float64
2.0    0.403226
1.0    0.330645
3.0    0.266129
dtype: float64
2.0    0.388889
1.0    0.333333
3.0    0.277778
dtype: float64<jupyter_text>Usar Grid Search com validação cruzada 5-Fold e diferentes parâmetros para diversos modelos e encontrar o melhor deles. Aplicar Standardization nos dados.<jupyter_code>from sklearn import svm
from sklearn import pipeline
ppl = pipeline.make_pipeline(preprocessing.StandardScaler(), 
                                  svm.SVC())
params = [{
    'svc__C': [10000, 0.001],
    'svc__kernel': ['linear', 'rbf']
}]
search_clf = model_selection.GridSearchCV(
    ppl, params, n_jobs=1, cv=5, verbose=2, iid=False
)
search_clf.fit(X_train, y_train
            )
search_clf.best_params_
search_clf.best_score_
search_clf.score(X_test, y_test)
y_pred = search_clf.best_estimator_.predict(X_test)
from sklearn import metrics
metrics.confusion_matrix(y_test, y_pred, labels=[1, 2, 3])
print(metrics.classification_report(y_test, y_pred, labels=[1,2,3]))
model = pipeline.make_pipeline(preprocessing.StandardScaler(),
                          svm.SVC(C=10000, kernel='rbf'))
print(model)
model.fit(X, y)
X[0
]
linha = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13])
X[0].shape
model.predict(linha.reshape(1,13))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.125103220478943, 'fraction_numerical': 0.05821635012386457, 'mean_word_length': 5.012406947890819, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '545170', 'license': 'MIT', 'n_tokens_mistral': 1022, 'n_tokens_neox': 955, 'n_words': 170, 'path': '2019/06-Tecnicas_para_Melhoria_de_Resultados/6-Pratica_Supervisionada.ipynb', 'repo_name': 'InsightLab/imersao-ciencia-de-dados-2019', 'size': '2422'}