-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2c.py
278 lines (206 loc) · 8.36 KB
/
2c.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_auc_score
from pprint import pprint
# Variables
global imp
imp = 0.1
max_depth = 10
# Import training set and sort values
df = pd.read_csv('seals_train.csv', sep=' ', header=None)
cols = ['Label']
paramCols = [i for i in range(1, len(df.columns))]
cols.extend(paramCols)
df.columns = cols
labels = df['Label']
df.drop('Label', inplace=True, axis=1)
df['Label'] = labels
# Functions for training tree
# Check purity of node
def check_purity(data):
entropy, _ = calculate_entropy(data)
if (len(np.unique(data[:, -1])) < 2) or (entropy <= imp):
return True
else:
return False
# Declare leaf node
def make_leaf(data):
label_column = data[:, -1]
unique_classes, counts_unique_classes = np.unique(label_column, return_counts=True)
majority_index = counts_unique_classes.argmax()
classification = unique_classes[majority_index]
return classification
# Check where there is unique values for potential splits
def get_potential_splits(data):
potential_splits = {}
_, n_columns = data.shape
for column_index in range(n_columns - 1): # excluding the last column which is the label
potential_splits[column_index] = []
values = data[:, column_index]
unique_values = np.unique(values)
for index in range(len(unique_values)):
if index != 0:
current_value = unique_values[index]
previous_value = unique_values[index - 1]
potential_split = (current_value + previous_value) / 2
potential_splits[column_index].append(potential_split)
return potential_splits
# Split group to two branches
def split_branch(data, split_column, split_value):
split_column_values = data[:, split_column]
left = data[split_column_values <= split_value]
right = data[split_column_values > split_value]
return left, right
# Calculate entropy of data
def calculate_entropy(data):
label_column = data[:, -1]
_, counts = np.unique(label_column, return_counts=True)
probabilities = counts / counts.sum()
entropy = sum(probabilities * -np.log2(probabilities))
return entropy, probabilities
# Calculate total entropy of proposed split
def calculate_total_entropy(left, right):
n = len(left) + len(right)
p_left = len(left) / n
p_right = len(right) / n
left_entropy, left_prob = calculate_entropy(left)
right_entropy, right_prob = calculate_entropy(right)
total_entropy = (p_left * left_entropy + p_right * right_entropy)
return total_entropy, left_prob, right_prob
# Check which of potential split that has lowest total entropy
def determine_best_split(data, potential_splits):
total_entropy = 9999
for column_index in potential_splits:
for value in potential_splits[column_index]:
left, right = split_branch(data, split_column=column_index, split_value=value)
current_total_entropy, left_prob, right_prob = calculate_total_entropy(left, right)
if current_total_entropy <= total_entropy:
total_entropy = current_total_entropy
best_split_column = column_index
best_split_value = value
left_prob = left_prob
right_prob = right_prob
return best_split_column, best_split_value, left_prob, right_prob
# Train/build/grow the tree
def build_decision_tree(df, counter=0, min_samples=2, max_depth=5, threshold=0.5):
global prob_array
prob_array = np.array([[0], [0]])
# Sorting data
if counter == 0:
global column_headers
column_headers = df.columns
data = df.values
else:
data = df
# Check if leaf
if (check_purity(data)) or (len(data) < min_samples) or (counter == max_depth):
classification = make_leaf(data)
return classification
# recursive part
else:
counter += 1
# helper functions
potential_splits = get_potential_splits(data)
split_column, split_value, left_prob, right_prob = determine_best_split(data, potential_splits)
left, right = split_branch(data, split_column, split_value)
# instantiate branches
feature_name = column_headers[split_column]
question = "{} <= {}".format(feature_name, split_value)
sub_branch = {question: []}
# Find answers (recursion)
yes_answer = build_decision_tree(left, counter, min_samples, max_depth, threshold)
no_answer = build_decision_tree(right, counter, min_samples, max_depth, threshold)
new_prob = np.array([[left_prob], [right_prob]])
prob_array = np.vstack((prob_array, new_prob))
# End or split
if yes_answer == no_answer:
sub_branch = yes_answer
else:
sub_branch[question].append(yes_answer)
sub_branch[question].append(no_answer)
return sub_branch
my_test_tree = build_decision_tree(df, max_depth=max_depth, threshold=0.90)
pprint(my_test_tree)
### Classification ###
# Import and sort test set
df_test = pd.read_csv('seals_train.csv', sep=' ', header=None)
cols = ['Label']
paramCols = [i for i in range(1, len(df_test.columns))]
cols.extend(paramCols)
df_test.columns = cols
labels = df_test['Label']
df_test.drop('Label', inplace=True, axis=1)
df_test['Label'] = labels
# Functions for classifying datapoints
def classify_test_set(test_set, tree):
question = list(tree.keys())[0]
feature_name, comparison_operator, value, _, left_prob, _, right_prob = question.split()
# ask question
if test_set[int(feature_name)] <= float(value):
answer = tree[question][0]
else:
answer = tree[question][1]
# base case
if not isinstance(answer, dict):
return answer
# recursive part
else:
residual_tree = answer
return classify_test_set(test_set, residual_tree)
def calculate_accuracy(df_test, tree):
df_test["classification"] = df_test.apply(classify_test_set, axis=1, args=(tree,))
df_test["classification_correct"] = df_test["classification"] == df_test["Label"]
accuracy = df_test["classification_correct"].mean()
return accuracy
# Make a confusion matrix
def my_confusion_matrix(r_test, test_pred):
matrix = np.zeros((2, 2)) # initialize the confusion matrix with zeros
for i in range(2):
for j in range(2):
matrix[i, j] = np.sum((r_test == test_pred[i]) & (test_pred == r_test[j]))
return matrix
# Accuracy from confusion matrix
def confusion_matrix_accuracy(confusion_matrix):
correct_class = confusion_matrix[0][0] + confusion_matrix[1][1]
n = correct_class + confusion_matrix[0][1] + confusion_matrix[1][0]
return correct_class/n
# Create ROC-curve
def roc_curve(df_test, prob):
labels = df_test['Label'].to_numpy()
# Creating vektors for TPR and FPR
FPR_vec = []
TPR_vec = []
accuracy = 0
best_threshold = 0
for threshold in np.linspace(0, 1, 100):
pred_labels = []
for probability in prob:
if probability[1] > threshold:
pred_labels.append(1)
else:
pred_labels.append(0)
confusion_matrix = my_confusion_matrix(labels, pred_labels)
tp = confusion_matrix[1][1]
fp = confusion_matrix[0][1]
tn = confusion_matrix[0][0]
TPR = tp / (tp + fp)
FPR = fp / (fp + tn) # calculate false positive rate
if confusion_matrix_accuracy(confusion_matrix) > accuracy:
accuracy = confusion_matrix_accuracy(confusion_matrix)
best_threshold = threshold
TPR_vec.append(TPR)
FPR_vec.append(FPR)
pred_labels = []
auc = roc_auc_score(FPR_vec, TPR_vec) # calculate area under the ROC curve
# Print the ROC curve with the AUC value.
plt.title('ROC')
plt.plot(FPR_vec, TPR_vec, 'r', label='AUC: %0.3f' % auc)
plt.xlabel('FPR')
plt.ylabel('TPR')
plt.legend()
plt.savefig('2c')
plt.show()
return round(best_threshold, 3)
best_threshold = roc_curve(df_test, prob_array)
print("Best calculated threshold is =", best_threshold)