-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecisonTree.py
163 lines (139 loc) · 6.26 KB
/
DecisonTree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from matplotlib.backends.backend_pdf import PdfPages
import pickle
from sklearn.preprocessing import LabelBinarizer
from sklearn.neural_network import MLPClassifier
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.metrics import RocCurveDisplay
import numpy as np
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import MinMaxScaler
from sklearn.compose import ColumnTransformer
from sklearn.pipeline import Pipeline
from sklearn.metrics import roc_curve, auc
from Model import *
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.metrics import mean_squared_error, r2_score
from sklearn import metrics
from sklearn.tree import export_graphviz
import graphviz
from sklearn.metrics import roc_auc_score
class DecisonTree(Model):
def __init__(self, name,
categorical_features=[],
continous_features=[],
classname='class',
fileModifer="Tree"
):
super().__init__(name,
categorical_features=categorical_features,
continous_features=continous_features,
classname=classname,
)
self.regression = False
self.modelName = "tree"
self.maxDepth = None
self.fileModifer = fileModifer
def setValidationData(self,data):
self.X_test = data.drop([self.classname], axis=1)
self.X_test = self.X_test.loc[:, ~ self.X.columns.str.contains('^Unnamed')]
self.y_test = data[self.classname]
def setTrainingData(self, data):
self.X_train = data.drop([self.classname], axis=1)
self.X_train = self.X_train.loc[:, ~ self.X.columns.str.contains('^Unnamed')]
self.y_train = data[self.classname]
def getModel(self):
if(self.regression):
return DecisionTreeRegressor(max_depth=self.maxDepth)
else:
return DecisionTreeClassifier(max_depth=self.maxDepth)
def evaluate(self, fileMod="tree"):
# Multiclass classification requires One verses Rest in order to compare ROC_AUC
self.y_score = self.clf.predict_proba(self.X_test)
# mean_path_length = get_mean_path_length(clf)
y_pred = self.clf.predict(self.X_test)
depth = self.clf['classifier'].tree_.max_depth
if self.regression:
self.print(f"Mean squared error: {mean_squared_error(self.y_test, y_pred)}")
self.print(f"Coefficient of determination: {r2_score(self.y_test, y_pred)}")
else:
self.roc_curves()
if (len(self.y_train.unique()) > 2):
auc_var = self.getMeanAuc()
else:
auc_var = roc_auc_score(self.y_test, self.y_score[:, 1])
self.print(f"AUC score: {auc_var}")
self.print(f'Accuracy: {metrics.accuracy_score(self.y_test, y_pred)}')
# Evaluate the accuracy of the model
self.print(f'Depth of the tree:{depth}')
if self.regression:
self.print("unable to save tree for regression")
else:
self.saveTreeClassifcationToFile(
self.clf, fileMod, classnames=self.y_train.unique())
def getMeanAuc(self):
# I hate i had to do this myself, but the roc_auc_score() cant handle having a varialbe test set for diffrent clasees
label_binarizer = LabelBinarizer().fit(self.y_train)
y_onehot_test = label_binarizer.transform(self.y_test)
aggragtedAuc = []
for class_of_interest in label_binarizer.classes_:
class_id = np.flatnonzero(
label_binarizer.classes_ == class_of_interest)[0]
y_onehot_test[:, class_id]
self.y_score[:, class_id]
fpr, tpr, _ = roc_curve(
y_onehot_test[:, class_id], self.y_score[:, class_id],
)
roc_auc = auc(fpr, tpr)
# print(f"{class_of_interest} auc: {roc_auc}")
aggragtedAuc.append(roc_auc)
return np.mean(aggragtedAuc)
def saveTreeClassifcationToFile(self, model, fileMod="tree", classnames=None):
dot_data = export_graphviz(model['classifier'],
out_file=None,
feature_names=model['preprocessor'].get_feature_names_out(
),
class_names=classnames,
filled=True,
rounded=True,
special_characters=True)
graph = graphviz.Source(dot_data)
graph.render(
f'decision_trees/{fileMod}-{self.fileModifer}-{self.name}')
def roc_curves(self):
num_classes = len(np.unique(self.y_train))
print(np.unique(self.y_train))
if num_classes == 2:
fpr, tpr, _ = roc_curve(
self.y_test, self.y_score[:, 1], pos_label=np.unique(self.y_train)[1])
roc_auc = auc(fpr, tpr)
roc_display = RocCurveDisplay(fpr=fpr, tpr=tpr, roc_auc=roc_auc,
estimator_name=self.name)
roc_display.plot()
plt.plot([0, 1], [0, 1], "k--", label="chance level (AUC = 0.5)")
plt.legend()
plt.savefig(f'ROC/{self.name}_nn_roc_curve.pdf')
plt.close()
else:
self.label_binarizer = LabelBinarizer().fit(self.y_train)
self.y_onehot_test = self.label_binarizer.transform(self.y_test)
with PdfPages(f'ROC/{self.name}_nn_roc_curve.pdf') as pdf:
for class_of_interest in self.label_binarizer.classes_:
class_id = np.flatnonzero(
self.label_binarizer.classes_ == class_of_interest)[0]
RocCurveDisplay.from_predictions(
self.y_onehot_test[:, class_id],
self.y_score[:, class_id],
name=f"{class_of_interest} vs the rest",
color="darkorange",
)
plt.plot([0, 1], [0, 1], "k--",
label="chance level (AUC = 0.5)")
plt.axis("square")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.title(f"{class_of_interest} vs the rest")
plt.legend()
pdf.savefig()
plt.close()