<jupyter_start><jupyter_text>Naive Bayes  Importando bibliotecas<jupyter_code>%matplotlib inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from sklearn.model_selection import KFold, cross_val_score
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.naive_bayes import GaussianNB
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics  import   accuracy_score 
from sklearn.model_selection import LeaveOneOut
from sklearn.model_selection import KFold

plt.style.use('ggplot')
pd.set_option('display.max_columns', 500) 
def report_teste(predictions, alg_name):

    print('Resultados para o classificador {0}:'.format(alg_name))
    print(classification_report(y_teste, predictions), 
    print ("Acurácia para o treino é ", accuracy_score(y_teste,predictions)))
    
def report_treino(predictions, alg_name):

    print('Resultados para o classificador {0}:'.format(alg_name))
    print(classification_report(y_treino, predictions), 
    print ("Acurácia para o treino é ", accuracy_score(y_treino,predictions)))<jupyter_output><empty_output><jupyter_text>Dados<jupyter_code>dataset = pd.read_csv('C:\\Users\\<NAME>\\Desktop\\PROVA\\classificacao_Q4.csv')
#Verificando a existência de missings
#dataset.apply(lambda x: x.isnull().sum())
dataset.head(5)
print("Dimensão dos nossos dados:\n", 
     dataset.shape)
#print("Tipo de variáveis:\n",
#     dataset.dtypes)<jupyter_output>Dimensão dos nossos dados:
 (1500, 101)<jupyter_text>Pocentagem da variável resposta<jupyter_code>resposta = dataset['target']
count = pd.DataFrame(resposta.value_counts())
percent = pd.DataFrame(resposta.value_counts(normalize = True)*100)
table = pd.concat([count, percent], axis = 1)
table.columns = ['# target', '% target']
table
#Descritiva de algumas variáveis
#dataset.describe()<jupyter_output><empty_output><jupyter_text>Treino e Teste<jupyter_code>feature_space = dataset.iloc[:, dataset.columns != 'target']
feature_class = dataset.iloc[:, dataset.columns == 'target']


X_treino, X_teste, y_treino, y_teste = train_test_split(feature_space,
                                                                    feature_class,
                                                                    test_size = 0.30, 
                                                                    random_state = 42)
# Limpar conjuntos de teste para evitar futuras mensagens de aviso
y_treino = y_treino.values.ravel() 
y_teste = y_teste.values.ravel()<jupyter_output><empty_output><jupyter_text>Ajustando Naive Bayes<jupyter_code>classifier = GaussianNB(priors=None)

classifier.fit(X_treino, y_treino)<jupyter_output><empty_output><jupyter_text>Precisão do classificador<jupyter_code>pred_test = classifier.predict(X_teste)
pred_train = classifier.predict(X_treino)<jupyter_output><empty_output><jupyter_text>Tabela com cálculo de vária métricas conjunto treino<jupyter_code>report_treino(pred_train,'Naive Bayes')<jupyter_output>Resultados para o classificador Naive Bayes:
Acurácia para o treino é  0.6885714285714286
             precision    recall  f1-score   support

        0.0       0.66      0.79      0.72       103
        1.0       0.58      0.68      0.63        98
        2.0       0.67      0.54      0.60       111
        3.0       0.86      0.69      0.76       105
        4.0       0.72      0.76      0.74       104
        5.0       0.73      0.55      0.62        97
        6.0       0.70      0.62      0.66        98
        7.0       0.76      0.67      0.71       111
        8.0       0.63      0.76      0.69       106
        9.0       0.67      0.81      0.73       117

avg / total       0.70      0.69      0.69      1050
 None<jupyter_text>Tabela com cálculo de vária métricas conjunto teste<jupyter_code>report_teste(pred_test,'Naive Bayes')<jupyter_output>Resultados para o classificador Naive Bayes:
Acurácia para o treino é  0.62
             precision    recall  f1-score   support

        0.0       0.62      0.85      0.72        47
        1.0       0.51      0.67      0.58        51
        2.0       0.60      0.50      0.55        42
        3.0       0.68      0.64      0.66        47
        4.0       0.63      0.48      0.54        46
        5.0       0.72      0.57      0.64        51
        6.0       0.79      0.53      0.64        51
        7.0       0.65      0.47      0.55        36
        8.0       0.63      0.77      0.69        47
        9.0       0.48      0.72      0.57        32

avg / total       0.64      0.62      0.62       450
 None<jupyter_text>Ajustando o classificador com Grid Search<jupyter_code>fit_nb = GaussianNB()
np.random.seed(42)
cv_kfold = KFold(10, shuffle = False)

param_grid = param_grid = {}
cv_nb = GridSearchCV(fit_nb,
                     cv = cv_kfold,
                     param_grid = param_grid, 
                     n_jobs = 3)
cv_nb.fit(X_treino, y_treino)
cv_nb.best_params_
fit_nb.set_params()
fit_nb.fit(X_treino, y_treino)<jupyter_output><empty_output><jupyter_text>Resultados Conjunto Treino<jupyter_code>pred_train2 = fit_nb.predict(X_treino)
report_treino(pred_train2, 'Naive Bayes com Grid Search')<jupyter_output>Resultados para o classificador Naive Bayes com Grid Search:
Acurácia para o treino é  0.6885714285714286
             precision    recall  f1-score   support

        0.0       0.66      0.79      0.72       103
        1.0       0.58      0.68      0.63        98
        2.0       0.67      0.54      0.60       111
        3.0       0.86      0.69      0.76       105
        4.0       0.72      0.76      0.74       104
        5.0       0.73      0.55      0.62        97
        6.0       0.70      0.62      0.66        98
        7.0       0.76      0.67      0.71       111
        8.0       0.63      0.76      0.69       106
        9.0       0.67      0.81      0.73       117

avg / total       0.70      0.69      0.69      1050
 None<jupyter_text>Resultados conjunto teste<jupyter_code>predictions_fit_nb = fit_nb.predict(X_teste)
report_teste(predictions_fit_nb, 'Naive Bayes com Grid Search')
predictions_nb = fit_nb.predict(X_teste)
print(confusion_matrix(y_teste, predictions_nb))
accuracy_nb = fit_nb.score(X_teste, y_teste)

print("Aqui está a nossa precisão média no conjunto de testes: {0:.3f}".format(accuracy_nb))
test_error_rate_nb = 1 - accuracy_nb
print("A taxa de erro de teste para o nosso modelo é: {0: .3f}" .format(test_error_rate_nb))<jupyter_output>A taxa de erro de teste para o nosso modelo é:  0.380<jupyter_text>Curva ROC<jupyter_code>predictions_prob = fit_nb.predict_proba(X_teste)[:, 1]

fpr2, tpr2, _ = roc_curve(y_teste,
                          predictions_prob,
                          pos_label = 1)
auc_nb = auc(fpr2, tpr2)
plt.figure()
lw = 2
plt.plot(fpr2, tpr2, color='darkorange',
         lw=lw, label='ROC curve (area = %0.2f)' % auc_nb)
plt.plot([0, 1], [0, 1], color='navy', lw=lw, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('Taxa de Falso Positivo')
plt.ylabel('Taxa de Verdadeiro Positivo')
plt.title('Receiver operating characteristic ')
plt.legend(loc="lower right")
plt.show()
    report_teste(predictions_nb, 'Naive Bayes')<jupyter_output>Resultados para o classificador Naive Bayes:
Acurácia para o treino é  0.62
             precision    recall  f1-score   support

        0.0       0.62      0.85      0.72        47
        1.0       0.51      0.67      0.58        51
        2.0       0.60      0.50      0.55        42
        3.0       0.68      0.64      0.66        47
        4.0       0.63      0.48      0.54        46
        5.0       0.72      0.57      0.64        51
        6.0       0.79      0.53      0.64        51
        7.0       0.65      0.47      0.55        36
        8.0       0.63      0.77      0.69        47
        9.0       0.48      0.72      0.57        32

avg / total       0.64      0.62      0.62       450
 None<jupyter_text>Validação Cruzada  K - fold<jupyter_code>X = dataset.iloc[:, 0:100].values
y = dataset['target'].astype('category')
from sklearn import model_selection
kfold = model_selection.KFold(n_splits=10, random_state=42)
model = GaussianNB()
scoring = 'accuracy'
results = model_selection.cross_val_score(model, X, y, cv=kfold, scoring=scoring)
results.mean(), results.std()<jupyter_output><empty_output><jupyter_text>LOOCV<jupyter_code>model = GaussianNB()
accuracies = cross_val_score(model, X=X, y=y, cv=LeaveOneOut())
accuracies.mean()<jupyter_output><empty_output><jupyter_text>Repeat CV<jupyter_code>from sklearn.model_selection import RepeatedKFold
cv_repeat = RepeatedKFold(n_splits=6, n_repeats=3, random_state=42)
model = GaussianNB()
accuracies = cross_val_score(model, X=X, y=y, cv=cv_repeat)
accuracies.mean()<jupyter_output><empty_output><jupyter_text>Separando as k primeiras observações para treino e o restante para teste<jupyter_code>X_treino = dataset.iloc[0:499, 0:99].values
y_treino = dataset.iloc[0:499, 100].values


X_teste = dataset.iloc[500:1500, 0:99].values
y_teste = dataset.iloc[500:1500, 100].values
clf = GaussianNB()
clf.fit(X_treino, y_treino)<jupyter_output><empty_output><jupyter_text>Precisão do classificador no Decision Tree<jupyter_code>pred_teste = clf.predict(X_teste)
pred_treino = clf.predict(X_treino)<jupyter_output><empty_output><jupyter_text>Métricas Treino<jupyter_code>report_treino(pred_treino, 'Naive Bayes')<jupyter_output>Resultados para o classificador Naive Bayes:
Acurácia para o treino é  0.751503006012024
             precision    recall  f1-score   support

        0.0       0.69      0.80      0.74        55
        1.0       0.62      0.75      0.68        48
        2.0       0.71      0.60      0.65        45
        3.0       0.85      0.76      0.80        58
        4.0       0.81      0.88      0.84        48
        5.0       0.80      0.61      0.69        46
        6.0       0.73      0.70      0.72        47
        7.0       0.88      0.71      0.78        51
        8.0       0.70      0.90      0.79        41
        9.0       0.79      0.80      0.79        60

avg / total       0.76      0.75      0.75       499
 None<jupyter_text>Métricas Teste<jupyter_code>report_teste(pred_teste, 'Naive Bayes')<jupyter_output>Resultados para o classificador Naive Bayes:
Acurácia para o treino é  0.607
             precision    recall  f1-score   support

        0.0       0.59      0.81      0.68        95
        1.0       0.52      0.55      0.54       101
        2.0       0.53      0.47      0.50       108
        3.0       0.65      0.65      0.65        94
        4.0       0.65      0.52      0.58       102
        5.0       0.67      0.49      0.56       102
        6.0       0.59      0.61      0.60       102
        7.0       0.68      0.52      0.59        96
        8.0       0.66      0.68      0.67       111
        9.0       0.59      0.80      0.68        89

avg / total       0.61      0.61      0.60      1000
 None
{'fraction_non_alphanumeric': 0.09290799315130216, 'fraction_numerical': 0.09462016761286834, 'mean_word_length': 1.8916102136529442, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '595436', 'license': 'Apache-2.0', 'n_tokens_mistral': 4436, 'n_tokens_neox': 3982, 'n_words': 870, 'path': 'Naive_Bayes.ipynb', 'repo_name': 'fabielfs/ML', 'size': '11106'}