<jupyter_start><jupyter_text>Random Forest  Importando bibliotecas<jupyter_code>%matplotlib inline

import time
import random
import numpy as np
import pandas as pd
import seaborn as sns
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.ensemble import RandomForestClassifier 
from urllib.request import urlopen 
from sklearn.metrics  import   accuracy_score 
from sklearn import tree 
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')
dataset.columns
names = dataset.columns
names_index = names[0:100]
names_index
#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 Random Forest<jupyter_code>#criterio = "gini"
criterio = "entropy"
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(criterion = criterio,
                                       max_depth=3,
                                       min_samples_leaf = 5, 
                                       min_samples_split = 10,   
                                       n_estimators=100, 
                                       max_features='auto', 
                                       oob_score=True, 
                                       random_state=42, 
                                       n_jobs=-1)
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,'treino')<jupyter_output>Resultados para o classificador treino:
Acurácia para o treino é  0.6361904761904762
             precision    recall  f1-score   support

        0.0       0.65      0.83      0.73       103
        1.0       0.56      0.55      0.56        98
        2.0       0.76      0.35      0.48       111
        3.0       0.70      0.69      0.69       105
        4.0       0.58      0.54      0.56       104
        5.0       0.71      0.57      0.63        97
        6.0       0.60      0.64      0.62        98
        7.0       0.61      0.68      0.64       111
        8.0       0.64      0.68      0.66       106
        9.0       0.63      0.82      0.71       117

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

        0.0       0.64      0.94      0.76        47
        1.0       0.46      0.51      0.49        51
        2.0       0.55      0.29      0.37        42
        3.0       0.65      0.70      0.67        47
        4.0       0.46      0.37      0.41        46
        5.0       0.76      0.57      0.65        51
        6.0       0.69      0.43      0.53        51
        7.0       0.53      0.58      0.55        36
        8.0       0.66      0.66      0.66        47
        9.0       0.41      0.75      0.53        32

avg / total       0.59      0.58      0.57       450
 None<jupyter_text>Ajustando o classificador com Grid Search<jupyter_code># random state para reproducibilidade
fit_rf = RandomForestClassifier(random_state=42)
np.random.seed(42)
cv_kfold = KFold(10, shuffle = False)

param_grid = {'max_depth': [2, 3, 4],
              'bootstrap': [True, False],
              'max_features': ['auto', 'sqrt'],  #'log2', None
              'criterion': ['gini', 'entropy']
             }
cv_rf = GridSearchCV(fit_rf,
                     cv = cv_kfold,
                     param_grid = param_grid, 
                     n_jobs = 3)
cv_rf.fit(X_treino, y_treino)
cv_rf.best_params_
fit_rf.set_params(criterion = 'gini',
                  max_features = 'auto', 
                  max_depth = 4,
                  bootstrap = True)<jupyter_output><empty_output><jupyter_text>Out of Bag Error Rate Podemos calcular a taxa de erro OOB e usá-la para avaliar quantas árvores são apropriadas para o nosso modelo<jupyter_code>import numpy as np
np.seterr(divide='ignore', invalid='ignore')
fit_rf.set_params(warm_start=True, 
                  oob_score=True)

min_estimators = 10
max_estimators = 600

error_rate = {}

for i in range(min_estimators, max_estimators + 1):
    fit_rf.set_params(n_estimators=i)
    fit_rf.fit(X_treino, y_treino)

    oob_error = 1 - fit_rf.oob_score_
    error_rate[i] = oob_error
# Convert dictionary to a pandas series for easy plotting 
oob_series = pd.Series(error_rate)
fig, ax = plt.subplots(figsize=(3, 3))

ax.set_facecolor('#fafafa')

oob_series.plot(kind='line',
                color = 'red')
plt.axhline(0.40, 
            color='#875FDB',
           linestyle='--')
plt.axhline(0.32, 
            color='#875FDB',
           linestyle='--')
plt.xlabel('n_estimators')
plt.ylabel('OOB Taxa de erro')
plt.title('OOB Taxa de erros em vários tamanhos de floresta \n(de 200 até 1000 árvores)')
print('OOB Taxa de erro para 300 árvores é: {0:.5f}'.format(oob_series[300]))
fit_rf.set_params(n_estimators=300,
                  bootstrap = True,
                  warm_start=False, 
                  oob_score=False)
fit_rf.fit(X_treino, y_treino)<jupyter_output><empty_output><jupyter_text>Resultados Conjunto Treino<jupyter_code>pred_train2 = fit_rf.predict(X_treino)
report_treino(pred_train2, 'Random Forest com Grid Search')<jupyter_output>Resultados para o classificador Random Forest com Grid Search:
Acurácia para o treino é  0.7323809523809524
             precision    recall  f1-score   support

        0.0       0.71      0.85      0.78       103
        1.0       0.65      0.63      0.64        98
        2.0       0.79      0.58      0.67       111
        3.0       0.89      0.72      0.80       105
        4.0       0.72      0.70      0.71       104
        5.0       0.79      0.66      0.72        97
        6.0       0.71      0.74      0.73        98
        7.0       0.79      0.76      0.77       111
        8.0       0.72      0.76      0.74       106
        9.0       0.65      0.89      0.75       117

avg / total       0.74      0.73      0.73      1050
 None<jupyter_text>Resultados conjunto teste<jupyter_code>predictions_fit_rf = fit_rf.predict(X_teste)
report_teste(predictions_fit_rf, 'Random Forest com Grid Search')<jupyter_output>Resultados para o classificador Random Forest com Grid Search:
Acurácia para o treino é  0.6244444444444445
             precision    recall  f1-score   support

        0.0       0.66      0.87      0.75        47
        1.0       0.54      0.61      0.57        51
        2.0       0.60      0.43      0.50        42
        3.0       0.71      0.68      0.70        47
        4.0       0.54      0.43      0.48        46
        5.0       0.69      0.53      0.60        51
        6.0       0.77      0.59      0.67        51
        7.0       0.61      0.61      0.61        36
        8.0       0.73      0.74      0.74        47
        9.0       0.44      0.78      0.56        32

avg / total       0.64      0.62      0.62       450
 None<jupyter_text>Variáveis Importantes<jupyter_code>def variable_importance(fit):
    importances = fit.feature_importances_
    indices = np.argsort(importances)[::-1]
    return {'importance': importances,
            'index': indices}
var_imp_rf = variable_importance(fit_rf)
importances_rf = var_imp_rf['importance']
indices_rf = var_imp_rf['index']
def print_var_importance(importance, indices, name_index):
    print("Ranking das variáveis mais importantes:")
    for f in range(0, 5):
        i = f
        print("{0}. A variável '{1}' tem uma diminuição média na impureza de {2:.5f}"
              .format(f + 1,
                      names_index[indices[i]],
                      importance[indices[f]]))
print_var_importance(importances_rf, indices_rf, names_index)
predictions_rf = fit_rf.predict(X_teste)
print(confusion_matrix(y_teste, predictions_rf))
accuracy_rf = fit_rf.score(X_teste, y_teste)

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

fpr2, tpr2, _ = roc_curve(y_teste,
                          predictions_prob,
                          pos_label = 1)
auc_rf = auc(fpr2, tpr2)
plt.figure()
lw = 2
plt.plot(fpr2, tpr2, color='darkorange',
         lw=lw, label='ROC curve (area = %0.2f)' % auc_rf)
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_rf, 'Random Forest')<jupyter_output>Resultados para o classificador Random Forest:
Acurácia para o treino é  0.6244444444444445
             precision    recall  f1-score   support

        0.0       0.66      0.87      0.75        47
        1.0       0.54      0.61      0.57        51
        2.0       0.60      0.43      0.50        42
        3.0       0.71      0.68      0.70        47
        4.0       0.54      0.43      0.48        46
        5.0       0.69      0.53      0.60        51
        6.0       0.77      0.59      0.67        51
        7.0       0.61      0.61      0.61        36
        8.0       0.73      0.74      0.74        47
        9.0       0.44      0.78      0.56        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 = RandomForestClassifier()
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 = RandomForestClassifier()
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 = RandomForestClassifier()
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
#criterio = 'gini'
criterio = 'entropy'
clf = RandomForestClassifier(criterion = criterio,
                                       max_depth=3,
                                       min_samples_leaf = 1, 
                                       min_samples_split = 10,   
                                       n_estimators=100, 
                                       max_features='auto', 
                                       oob_score=True, 
                                       random_state=42, 
                                       n_jobs=-1)
clf.fit(X_treino, y_treino)<jupyter_output><empty_output><jupyter_text>Precisão do classificador no Random Forest<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, 'Random Forest')<jupyter_output>Resultados para o classificador Random Forest:
Acurácia para o treino é  0.687374749498998
             precision    recall  f1-score   support

        0.0       0.59      0.85      0.70        55
        1.0       0.65      0.77      0.70        48
        2.0       0.89      0.38      0.53        45
        3.0       0.68      0.81      0.74        58
        4.0       0.81      0.44      0.57        48
        5.0       0.83      0.54      0.66        46
        6.0       0.60      0.79      0.68        47
        7.0       0.74      0.67      0.70        51
        8.0       0.90      0.63      0.74        41
        9.0       0.64      0.87      0.74        60

avg / total       0.72      0.69      0.68       499
 None<jupyter_text>Métricas Teste<jupyter_code>report_teste(pred_teste, 'Random Forest')<jupyter_output>Resultados para o classificador Random Forest:
Acurácia para o treino é  0.57
             precision    recall  f1-score   support

        0.0       0.55      0.80      0.65        95
        1.0       0.56      0.58      0.57       101
        2.0       0.65      0.29      0.40       108
        3.0       0.56      0.76      0.64        94
        4.0       0.63      0.24      0.34       102
        5.0       0.68      0.42      0.52       102
        6.0       0.55      0.75      0.64       102
        7.0       0.56      0.52      0.54        96
        8.0       0.73      0.55      0.63       111
        9.0       0.46      0.88      0.60        89

avg / total       0.60      0.57      0.55      1000
 None
{'fraction_non_alphanumeric': 0.09151329243353783, 'fraction_numerical': 0.08403629856850715, 'mean_word_length': 1.7766146202980837, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 92, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '21', 'dir': 'jupyter-structured-clean-dedup', 'id': '97353', 'license': 'Apache-2.0', 'n_tokens_mistral': 5964, 'n_tokens_neox': 5333, 'n_words': 1178, 'path': 'Random Forest 2.0.ipynb', 'repo_name': 'fabielfs/ML', 'size': '15657'}