This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
decision_tree.py
195 lines (182 loc) · 10.2 KB
/
decision_tree.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from openml.apiconnector import APIConnector
from scipy.io.arff import loadarff
import pandas as pd
import matplotlib.pylab as plt
import os
import numpy as np
from sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier, export_graphviz
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.metrics import fbeta_score, confusion_matrix, roc_curve, get_scorer
from subprocess import check_output
home_dir = os.path.expanduser("~")
openml_dir = os.path.join(home_dir, ".openml")
cache_dir = os.path.join(openml_dir, "cache")
with open(os.path.join(openml_dir, "apikey.txt"), 'r') as fh:
key = fh.readline().rstrip('\n')
fh.close()
## load dataset lists
openml = APIConnector(cache_directory = cache_dir, apikey = key)
# datasets = openml.get_dataset_list()
# data = pd.DataFrame(datasets)
dataset = openml.download_dataset(10)
# print('Data-set name: %s'%dataset.name)
# print(dataset.description)
data, meta = loadarff(dataset.data_file)
target_attribute = dataset.default_target_attribute
target_attribute_names = meta[target_attribute][1]
X, y, attribute_names = dataset.get_dataset(target = target_attribute, return_attribute_names = True)
y_values = np.unique(y)
print('y_values%s'%y_values)
fig, axes_bar = plt.subplots(1, 1)
# plot the distribution of target attribute
y_values_counts, bin_edges = np.histogram(y, y_values.size, density = False)
print('y_values_counts%s'%y_values_counts, bin_edges)
target_attribute_names = np.array(target_attribute_names)
print('target_attribute_names%s'%target_attribute_names)
# the x locations for the groups
ind = np.arange(y_values.size)
# the width of the bars
bar_width = 0.35
rects = axes_bar.bar(ind, y_values_counts, bar_width, color = 'green', alpha = 0.8, edgecolor = 'green')
# attach some text labels
for rect in rects:
height = rect.get_height()
axes_bar.text(rect.get_x() + rect.get_width() / 2.0, height, '%d' % int(height), ha='center', va='bottom')
axes_bar.set_xlim(-bar_width, len(ind) + bar_width)
axes_bar.set_title('Histogram of ' + str(target_attribute) + ' of ' + dataset.name + ' dataset', fontsize = 'x-large')
axes_bar.set_xticks(ind + bar_width / 2.0)
axes_bar.set_xticklabels(target_attribute_names, fontsize = 'medium')
axes_bar.set_xlabel('Class', fontsize = 'large')
axes_bar.set_ylabel('Count', fontsize = 'large')
# explore features
for index, attribute in enumerate(attribute_names):
print(attribute)
print(np.histogram2d(X[:, index], y, bins = [np.unique(X[:, index]).size, np.unique(y).size])[0])
# fig_explore, axes_explore = plt.subplots(1, 1)
# axes_explore.scatter(X[:, index], y)
## remove the smaller classes to get a binary problem
kept_y_values = np.sort(y_values[np.argsort(y_values_counts)[-2:]])
print('kept_y_values%s'%kept_y_values)
select_indices = np.where(np.in1d(y, kept_y_values))
# select_indices = np.where(np.logical_or(y == kept_y_values[0], y == kept_y_values[1]))
bi_class_X, bi_class_y = X[select_indices], y[select_indices] - 1
bi_class_target_attrs = target_attribute_names[kept_y_values]
print('bi_class_target_attrs%s'%bi_class_target_attrs)
## To evaluate the performance, we get the seventy percent of data as training data
## and the remaining thirty percent as test data
rnd_indices = np.random.permutation(len(bi_class_X))
rnd_training_X, rnd_training_y = bi_class_X[rnd_indices[:int(len(rnd_indices) * 0.7)]], bi_class_y[rnd_indices[:int(len(rnd_indices) * 0.7)]]
rnd_test_X, rnd_test_y = bi_class_X[rnd_indices[int(len(rnd_indices) * 0.7):]], bi_class_y[rnd_indices[int(len(rnd_indices) * 0.7):]]
## CART with default setting
clf_cart = DecisionTreeClassifier()
clf_cart.fit(rnd_training_X, rnd_training_y)
export_graphviz(clf_cart, out_file = "default_cart.dot", feature_names = attribute_names,
class_names = bi_class_target_attrs,
filled = True, rounded = True,
special_characters = True)
print(check_output('dot -Tpdf default_cart.dot -o default_cart.pdf', shell = True))
print("Accuracy = %s"%accuracy_score(rnd_test_y, clf_cart.predict(rnd_test_X)))
print("Precision = %s"%precision_score(rnd_test_y, clf_cart.predict(rnd_test_X)))
print("Recall = %s"%recall_score(rnd_test_y, clf_cart.predict(rnd_test_X)))
print("F = %s"%fbeta_score(rnd_test_y, clf_cart.predict(rnd_test_X), beta=1))
print("Confusion matrix = %s"%confusion_matrix(rnd_test_y, clf_cart.predict(rnd_test_X)))
roc_auc_scorer = get_scorer("roc_auc")
print("ROC AUC = %s"%roc_auc_scorer(clf_cart, rnd_test_X, rnd_test_y))
fpr, tpr, thresholds = roc_curve(rnd_test_y, clf_cart.predict_proba(rnd_test_X)[:, 1])
figt_roc, axes_roc = plt.subplots(1, 1)
axes_roc.plot(fpr, tpr, label = 'CART-1')
# CART with max_depth = 4, min_samples_leaf = 5
clf_cart = DecisionTreeClassifier(max_depth = 4, min_samples_leaf = 5)
clf_cart.fit(rnd_training_X, rnd_training_y)
export_graphviz(clf_cart, out_file = "cart.dot", feature_names = attribute_names,
class_names = bi_class_target_attrs,
filled = True, rounded = True,
special_characters = True)
print(check_output('dot -Tpdf cart.dot -o cart.pdf', shell = True))
print("Accuracy = %s"%accuracy_score(rnd_test_y, clf_cart.predict(rnd_test_X)))
print("Precision = %s"%precision_score(rnd_test_y, clf_cart.predict(rnd_test_X)))
print("Recall = %s"%recall_score(rnd_test_y, clf_cart.predict(rnd_test_X)))
print("F = %s"%fbeta_score(rnd_test_y, clf_cart.predict(rnd_test_X), beta=1))
print("Confusion matrix = %s"%confusion_matrix(rnd_test_y, clf_cart.predict(rnd_test_X)))
roc_auc_scorer = get_scorer("roc_auc")
print("ROC AUC = %s"%roc_auc_scorer(clf_cart, rnd_test_X, rnd_test_y))
fpr, tpr, thresholds = roc_curve(rnd_test_y, clf_cart.predict_proba(rnd_test_X)[:, 1])
axes_roc.plot(fpr, tpr, label = 'CART-2')
## randomized tree with default setting
clf_rnd_tree = ExtraTreeClassifier()
clf_rnd_tree.fit(rnd_training_X, rnd_training_y)
export_graphviz(clf_rnd_tree, out_file = 'default_rnd_tree.dot',
feature_names = attribute_names,
class_names = bi_class_target_attrs,
filled = True, rounded = True,
special_characters = True)
print(check_output('dot -Tpdf default_rnd_tree.dot -o default_rnd_tree.pdf', shell = True))
print("Accuracy = %s"%accuracy_score(rnd_test_y, clf_rnd_tree.predict(rnd_test_X)))
print("Precision = %s"%precision_score(rnd_test_y, clf_rnd_tree.predict(rnd_test_X)))
print("Recall = %s"%recall_score(rnd_test_y, clf_rnd_tree.predict(rnd_test_X)))
print("F = %s"%fbeta_score(rnd_test_y, clf_rnd_tree.predict(rnd_test_X), beta=1))
print("Confusion matrix = %s"%confusion_matrix(rnd_test_y, clf_rnd_tree.predict(rnd_test_X)))
fpr, tpr, thresholds = roc_curve(rnd_test_y, clf_rnd_tree.predict_proba(rnd_test_X)[:, 1])
axes_roc.plot(fpr, tpr, label = "Randomized tree-1")
axes_roc.set_title("ROC of CART and a randomized tree")
axes_roc.set_xlabel("FPR")
axes_roc.set_ylabel("TPR")
axes_roc.set_ylim(0, 1.1)
axes_roc.legend(loc = 'best', fontsize = 'medium')
roc_auc_scorer = get_scorer("roc_auc")
print("ROC AUC = %s"%roc_auc_scorer(clf_rnd_tree, rnd_test_X, rnd_test_y))
# randomized tree with max_depth = 4, min_samples_leaf = 5
clf_rnd_tree = ExtraTreeClassifier(max_depth = 4, min_samples_leaf = 5)
clf_rnd_tree.fit(rnd_training_X, rnd_training_y)
export_graphviz(clf_rnd_tree, out_file = 'rnd_tree.dot',
feature_names = attribute_names,
class_names = bi_class_target_attrs,
filled = True, rounded = True,
special_characters = True)
print(check_output('dot -Tpdf rnd_tree.dot -o rnd_tree.pdf', shell = True))
print("Accuracy = %s"%accuracy_score(rnd_test_y, clf_rnd_tree.predict(rnd_test_X)))
print("Precision = %s"%precision_score(rnd_test_y, clf_rnd_tree.predict(rnd_test_X)))
print("Recall = %s"%recall_score(rnd_test_y, clf_rnd_tree.predict(rnd_test_X)))
print("F = %s"%fbeta_score(rnd_test_y, clf_rnd_tree.predict(rnd_test_X), beta=1))
print("Confusion matrix = %s"%confusion_matrix(rnd_test_y, clf_rnd_tree.predict(rnd_test_X)))
fpr, tpr, thresholds = roc_curve(rnd_test_y, clf_rnd_tree.predict_proba(rnd_test_X)[:, 1])
axes_roc.plot(fpr, tpr, label = "Randomized tree-2")
axes_roc.set_title("ROC of CART and a randomized tree")
axes_roc.set_xlabel("FPR")
axes_roc.set_ylabel("TPR")
axes_roc.set_ylim(0, 1.1)
axes_roc.legend(loc = 'best', fontsize = 'medium')
roc_auc_scorer = get_scorer("roc_auc")
print("ROC AUC = %s"%roc_auc_scorer(clf_rnd_tree, rnd_test_X, rnd_test_y))
## study how stable the trees returned by CART
fig_rnd_tree_roc, axes_rnd_tree = plt.subplots()
for i in xrange(5):
rnd_indices = np.random.permutation(len(bi_class_X))
training_indices = rnd_indices[:int(len(rnd_indices) * 0.7)]
test_indices = rnd_indices[int(len(rnd_indices) * 0.7):]
training_X, training_y = bi_class_X[training_indices], bi_class_y[training_indices]
test_X, test_y = bi_class_X[test_indices], bi_class_y[test_indices]
clf_rnd_cart = DecisionTreeClassifier(max_depth = 4, min_samples_leaf = 5)
clf_rnd_cart.fit(training_X, training_y)
output_file_name = 'cart_run' + str(i + 1) + '.dot'
export_graphviz(clf_rnd_cart, out_file = output_file_name,
feature_names = attribute_names,
class_names = bi_class_target_attrs,
filled=True, rounded=True,
special_characters=True)
print(check_output('dot -Tpdf ' + output_file_name + ' -o ' + output_file_name.replace('.dot', '.pdf'), shell = True))
print("Accuracy = %s"%accuracy_score(test_y, clf_rnd_cart.predict(test_X)))
print("Precision = %s"%precision_score(test_y, clf_rnd_cart.predict(test_X)))
print("Recall = %s"%recall_score(test_y, clf_rnd_cart.predict(test_X)))
print("F = %s"%fbeta_score(test_y, clf_rnd_cart.predict(test_X), beta=1))
print("Confusion matrix = %s"%confusion_matrix(test_y, clf_rnd_cart.predict(test_X)))
fpr, tpr, thresholds = roc_curve(test_y, clf_rnd_cart.predict_proba(test_X)[:, 1])
axes_rnd_tree.plot(fpr, tpr, label = 'CART-' + str(i + 1))
roc_auc_scorer = get_scorer("roc_auc")
print("ROC AUC = %s"%roc_auc_scorer(clf_rnd_cart, test_X, test_y))
axes_rnd_tree.set_title("ROCs of five CART")
axes_rnd_tree.set_xlabel("FPR")
axes_rnd_tree.set_ylabel("TPR")
axes_rnd_tree.set_ylim(0, 1.1)
axes_rnd_tree.legend(loc = 'best', fontsize = 'medium')
plt.show()