<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>Data Preprocessing Libraries<jupyter_code>from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn import preprocessing
from sklearn.preprocessing import OrdinalEncoder<jupyter_output><empty_output><jupyter_text>Feature Selection & Modelling Libraries<jupyter_code>from sklearn.model_selection import train_test_split
from sklearn.feature_selection import SelectPercentile, f_classif
from sklearn.tree import DecisionTreeClassifier
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<jupyter_output><empty_output><jupyter_text>Model Building<jupyter_code>from sklearn import tree
# Acessing the data
!wget "https://github.com/univai-ghf/ghfmedia/raw/main/data/Trees_and_Ensembles/datasets.rar" 
# Extracting the dataset
!mkdir './datasets'
!unrar e './datasets.rar' 'datasets'

fl_out = "./datasets"
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","sel_cols"]

for i in list_objs:
     globals()[i]= pick_in(i)
###run till here (3 mins)
df_all_train2.shape
df_all_train2[0:10,-6:].astype('int')<jupyter_output><empty_output><jupyter_text>Building Model  Measure<jupyter_code>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
####Single Decision Tree
DT = DecisionTreeClassifier(random_state=0,max_depth=2)
DT.fit(df_all_train2,y_train1)
auc1_te = auc1_scr("DT",df_all_test2,y_test1)
auc1_tr = auc1_scr("DT",df_all_train2,y_train1)
auc1_te,auc1_tr
fig = plt.figure(figsize=(40,40))
_=tree.plot_tree(DT,feature_names=sel_cols,  
                   
                   filled=True,max_depth=3)

fig.savefig("decistion_tree.png")
auc1_te = auc1_scr("DT",df_all_test2,y_test1)
auc1_tr = auc1_scr("DT",df_all_train2,y_train1)
auc1_te,auc1_tr
####Back to presentation
###Exercises
# Make Decision Tree Classifier with random state=0, max_depth=50 & max_depth=100
# Do you observe any significant changes in the train and test auc score ?
# How will you justify these changes
# Your code here
DT_5 = DecisionTreeClassifier(__________)
DT_5.fit(_____________)

auc1_te = auc1_scr(___________)
auc1_tr = auc1_scr(____________)

print(________________________)
# Now try to make a Decision Tree Classifier with max_depth =100
# compare the results with the earlier one and try to explain the reson for the changes
# Your code here




##back to slide
dt_grid= DecisionTreeClassifier(random_state=42)

params = {
    'max_depth': [2, 3,5],
    'min_samples_leaf': [20, 50, 100],
    'criterion': ["gini", "entropy"]
}

grid_search = GridSearchCV(estimator=dt_grid, 
                           param_grid=params, 
                           cv=2, n_jobs=-1, verbose=1, scoring = "accuracy")


grid_search.fit(df_all_train2,y_train1)

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.09790562705021448, 'fraction_numerical': 0.022457734039868785, 'mean_word_length': 6.065953654188949, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '454650', 'license': 'MIT', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1389, 'n_words': 285, 'path': 'TreesEnsembles_part1.ipynb', 'repo_name': 'univai-ghf/TreesAndEnsembleWorkshop', 'size': '3963'}