<jupyter_start><jupyter_text>Data inputs and Display Libraries<jupyter_code>import pandas as pd
import numpy as np
import pickle

pd.set_option('display.float_format', lambda x: '%.5f' % x)
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'<jupyter_output><empty_output><jupyter_text>Modelling Libraries<jupyter_code>from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn import model_selection

from xgboost import XGBClassifier
import pickle
from sklearn.model_selection import GridSearchCV<jupyter_output><empty_output><jupyter_text>Metrics Libraries<jupyter_code>from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix,ConfusionMatrixDisplay
from sklearn.metrics import roc_curve
from sklearn.metrics import auc
from sklearn.metrics import f1_score
from matplotlib import pyplot
import matplotlib.pyplot as plt
from sklearn.metrics import average_precision_score
from sklearn.metrics import precision_recall_curve
# Acessing the data
!wget "https://github.com/univai-ghf/ghfmedia/raw/main/data/Trees_and_Ensembles/datasets.rar" 
# Extracting the dataset
!mkdir './workshop_trees'
!unrar e './datasets.rar' 'workshop_trees'
fl_out = "./workshop_trees"
def pick_in(obj_name):
    fl_out1 = fl_out + "/" + obj_name
    pickle_in = open(fl_out1,"rb")
    mod1= pickle.load(pickle_in)
    
    return mod1
list_objs = ["df_all_train2","y_train1","df_all_test2","y_test1"]

for i in list_objs:
     globals()[i]= pick_in(i)
def auc1_scr(mod1,test_set,actual1):
    mod = eval(mod1)
    pred1=mod.predict_proba(test_set)[:,1]
    fpr, tpr, thresholds = roc_curve(actual1, pred1)
    auc1 = auc(fpr, tpr)
    
    return auc1
###bagging classifier

bc = BaggingClassifier(random_state=0)
bc.fit(df_all_train2,y_train1)
auc1_te = auc1_scr("bc",df_all_test2,y_test1)
auc1_tr = auc1_scr("bc",df_all_train2,y_train1)
auc1_te,auc1_tr
kfold = model_selection.KFold(n_splits = 3)
  
results = model_selection.cross_val_score(bc, df_all_test2,y_test1, 
                                          cv = kfold,scoring='roc_auc',n_jobs=-1)
print (results.mean())
###random forest
rf = RandomForestClassifier( random_state=0)
rf.fit(df_all_train2,y_train1)
auc1_te = auc1_scr("rf",df_all_test2,y_test1)
auc1_tr = auc1_scr("rf",df_all_train2,y_train1)
auc1_te,auc1_tr
kfold = model_selection.KFold(n_splits = 3)
  
results = model_selection.cross_val_score(rf, df_all_test2,y_test1, 
                                          cv = kfold,scoring='roc_auc',n_jobs=-1)
results.mean()<jupyter_output><empty_output><jupyter_text>Grid Search<jupyter_code>rf_grid= RandomForestClassifier(random_state=42)

params = {
    'n_estimators': [50,100],
    'criterion': ["gini", "entropy"],
    'max_samples': [0.2,0.5],
    'max_features':[0.2,0.3]
    
}

grid_search = GridSearchCV(estimator=rf_grid, 
                           param_grid=params, 
                           cv=2, n_jobs=5, verbose=1, scoring = "roc_auc")


grid_search.fit(df_all_test2,y_test1)
score_df = pd.DataFrame(grid_search.cv_results_)
score_df.head()
score_df.sort_values(["rank_test_score"]).head(5)
grid_search.best_estimator_<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10731857318573186, 'fraction_numerical': 0.023062730627306273, 'mean_word_length': 5.243761996161228, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '467647', 'license': 'MIT', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1216, 'n_words': 198, 'path': 'TreesEnsembles_part2.ipynb', 'repo_name': 'univai-ghf/TreesAndEnsembleWorkshop', 'size': '3252'}