<jupyter_start><jupyter_text>**Importing the libraries**<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import confusion_matrix, classification_report, f1_score, accuracy_score, roc_auc_score
from sklearn.model_selection import cross_val_score<jupyter_output><empty_output><jupyter_text>**Importing the dataset**<jupyter_code>df = pd.read_csv("/content/Social_Network_Ads.csv")
df
# data cleaning
df = df.drop('User ID',axis=1)
# encoding
le = LabelEncoder()
gender = le.fit_transform(df['Gender'])
df.Gender = gender
df.head()<jupyter_output><empty_output><jupyter_text>**Splitting the dataset into the Training set and Test set**<jupyter_code>X = df.iloc[:,:3].values
y = df.iloc[:,-1].values
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)<jupyter_output><empty_output><jupyter_text>**Feature Scaling**<jupyter_code>scaler = StandardScaler()

X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)<jupyter_output><empty_output><jupyter_text>**Fitting Decision Tree Classification to the Training set**<jupyter_code># finding the best depth
for depth in [1,2,3,4,5,6,7,8,9,10,20,40,60]:
  
  dt = DecisionTreeClassifier(max_depth=depth) 
  dt.fit(X_train, y_train) 
  trainAccuracy = accuracy_score(y_test, dt.predict(X_test))  # to detect overfitting, we need both train_data accuracy, test_data accuracy
  
  dt = DecisionTreeClassifier(max_depth=depth)   # a fresh model which is not trained yet
  valAccuracy = cross_val_score(dt, X, y, cv=10)    # test_dataset accuracy
  
  print("depth: ",depth, "train_accuracy: ", trainAccuracy, "validation_accuracy: ",np.mean(valAccuracy))<jupyter_output>depth:  1 train_accuracy:  0.8375 validation_accuracy:  0.825
depth:  2 train_accuracy:  0.8625 validation_accuracy:  0.8949999999999999
depth:  3 train_accuracy:  0.875 validation_accuracy:  0.85
depth:  4 train_accuracy:  0.8375 validation_accuracy:  0.8724999999999999
depth:  5 train_accuracy:  0.85 validation_accuracy:  0.865
depth:  6 train_accuracy:  0.8625 validation_accuracy:  0.8625
depth:  7 train_accuracy:  0.8 validation_accuracy:  0.86
depth:  8 train_accuracy:  0.8125 validation_accuracy:  0.85
depth:  9 train_accuracy:  0.8125 validation_accuracy:  0.8525
depth:  10 train_accuracy:  0.7875 validation_accuracy:  0.8475000000000001
depth:  20 train_accuracy:  0.8 validation_accuracy:  0.8474999999999999
depth:  40 train_accuracy:  0.8 validation_accuracy:  0.845
depth:  60 train_accuracy:  0.8125 validation_accuracy:  0.8375<jupyter_text>Interpretation of the above:* at depth=2, we get maximum cv score<jupyter_code># applying the best depth
dt = DecisionTreeClassifier(max_depth=2)
dt.fit(X_train,y_train)<jupyter_output><empty_output><jupyter_text>**Predicting the Test set results**<jupyter_code>y_pred = dt.predict(X_test)
# comparing

compare = pd.DataFrame({'actual':y_test,'predicted':y_pred})
compare.head()<jupyter_output><empty_output><jupyter_text>**Making the Confusion Matrix**<jupyter_code>cm = confusion_matrix(y_test,y_pred)
print("confusion matrix:")
print(cm)

auc = roc_auc_score(y_test,y_pred)
print("auroc score: ", auc)
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       1.00      0.81      0.89        57
           1       0.68      1.00      0.81        23

    accuracy                           0.86        80
   macro avg       0.84      0.90      0.85        80
weighted avg       0.91      0.86      0.87        80<jupyter_text>* auroc score = 0.90* f1 score = 0.86* This shows that the model is a good model **Visualising the Training set results**<jupyter_code>from sklearn import tree
dt = DecisionTreeClassifier(max_depth=2)
dt.fit(X_train,y_train)
tree.plot_tree(dt)<jupyter_output><empty_output><jupyter_text>**Visualising the Test set results**<jupyter_code>from sklearn import tree
dt = DecisionTreeClassifier(max_depth=2)
dt.fit(X_test,y_test)
tree.plot_tree(dt)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10213270142180095, 'fraction_numerical': 0.06208530805687204, 'mean_word_length': 5.424657534246576, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '452106', 'license': 'Apache-2.0', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1508, 'n_words': 316, 'path': 'Task - 7/Assignment 11 to 16/Decision_Trees_Assign15_task7.ipynb', 'repo_name': 'sidharth-ds/Guvi_Tasks', 'size': '4220'}