<jupyter_start><jupyter_text><NAME> - 24986<jupyter_code># Decision Tree Classifier for Bank Marketing Dataset 
# navigate to directory
pwd
cd Desktop/Sabancı/"CS 412"/HW2
import numpy as np
import sys # for interpreter-function-library accesses 
from scipy import stats
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# necessary libraries imported 
# import the dataset from the archive
bank_data = pd.read_csv("bank-full.csv", sep = ";")
bank_data.head() 
# y is our target variable, change the name for ease
bank_data = bank_data.rename(columns = {"y" : "Target"})
bank_data.head()
# split the dataset into two groups dependent(target) variable and independent variables(features)
feature_cols = ["age", "job", "marital", "education", "default", "balance", "housing", "loan", "contact", "day", "month", "duration", "campaign", "pdays", "previous", "poutcome"] 
X = bank_data[feature_cols] # features 
y = bank_data["Target"] # target variable
from sklearn.tree import DecisionTreeClassifier 
from sklearn.model_selection import train_test_split
from sklearn import metrics # for accuracy
# split the data into training and test set 

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 1)
X_train.dtypes # have to convert object datatypes using labelencoder before using fit()
from sklearn import preprocessing # to use for labelencoder(), preprocess both sets 

def convert(data): 
    le = preprocessing.LabelEncoder()
    for i in data:
        if(data[i].dtype == object):
            data[i] = le.fit_transform(data[i])
        else:
            pass
    return data 
X_train = convert(X_train)
X_test = convert(X_test)
tree = DecisionTreeClassifier() # Decision Tree Object, max_depth created max by default for first depth tryout 

tree = tree.fit(X_train, y_train) # Train decision tree classifier 

y_pred = tree.predict(X_test) # predict the outcome for test set
# calculate model accuracy, how often is classifier accurate?

print("Accuracy of the model is ", metrics.accuracy_score(y_test, y_pred))
from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred)) # just additional info, not really needed 
# f1 score is almost the same as accuracy, meaning we have an even distribution 
# if that wasn't the case; f1-score would provide better results compared to accuracy metric<jupyter_output><empty_output><jupyter_text>- We can also specify the parameters for our decision tree model such as max_depth manually...<jupyter_code># reconstructing the tree to measure if max_depth specification changes the accuracy
# use a for loop to try different depths
for i in range(1,11,1):
    clf = DecisionTreeClassifier(max_depth = i)
    clf = clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test)
    print("Accuracy of the model with max_depth = ",i," ",float("%.5f" % metrics.accuracy_score(y_test, y_pred)))<jupyter_output>Accuracy of the model with max_depth =  1   0.88454
Accuracy of the model with max_depth =  2   0.89047
Accuracy of the model with max_depth =  3   0.89074
Accuracy of the model with max_depth =  4   0.89215
Accuracy of the model with max_depth =  5   0.89578
Accuracy of the model with max_depth =  6   0.89773
Accuracy of the model with max_depth =  7   0.89888
Accuracy of the model with max_depth =  8   0.89985
Accuracy of the model with max_depth =  9   0.90065
Accuracy of the model with max_depth =  10   0.89879
{'fraction_non_alphanumeric': 0.08441746169413125, 'fraction_numerical': 0.02630818155536282, 'mean_word_length': 4.616883116883117, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '244420', 'license': 'MIT', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1065, 'n_words': 397, 'path': 'dtClassifier/decision_tree_classifier_code_n_analysis.ipynb', 'repo_name': 'kaanguney/machineLearning', 'size': '3472'}