Repository navigation
Expand file tree
/
Copy path2b.py
More file actions
220 lines (157 loc) · 6.52 KB
/
Copy path2b.py
File metadata and controls
220 lines (157 loc) · 6.52 KB
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
import pandas as pd
import numpy as np
from pprint import pprint
# Variabler
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):
if (len(np.unique(data[:, -1])) < 2) or (calculate_entropy(data) <= 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): # Stop before column with 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 branch when we have determined best split
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 the entropy of group 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
# Calculate total entropy of both branches
def calculate_total_entropy(left, right):
n = len(left) + len(right)
p_left = len(left) / n
p_right = len(right) / n
total_entropy = (p_left * calculate_entropy(left)
+ p_right * calculate_entropy(right))
return total_entropy
# Find the splits with lowest total entropy
def determine_best_split(data, potential_splits):
# Start with an impossible high value that calc entropy won't be higher than
first_iter = True
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 = calculate_total_entropy(left, right)
if first_iter == True or current_total_entropy <= total_entropy:
first_iter = False
total_entropy = current_total_entropy
best_split_column = column_index
best_split_value = value
return best_split_column, best_split_value
# Train/build/grow tree on the trainingset
def build_decision_tree(df, counter=0, min_samples=2, max_depth=5):
# Sorting data
if counter == 0:
global column_headers
column_headers = df.columns
data = df.values
else:
data = df
# For task 2b
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 = determine_best_split(data, potential_splits)
left, right = split_branch(data, split_column, split_value)
# instantiate sub-tree
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)
no_answer = build_decision_tree(right, counter, min_samples, max_depth)
# Check if we're at the end or append 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
# build tree with the training set
my_tree = build_decision_tree(df, max_depth=max_depth)
### 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 = 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
# Check accuracy of decision tree
accuracy_test = calculate_accuracy(df_test, my_tree)
print("Accuracy of decision tree on test set = %.3f." % accuracy_test)
# Some tests for determining depth and threshold for purity
#accuracy_train = calculate_accuracy(df, my_tree)
#total_accuracy = accuracy_test + accuracy_train
#confusion_matrix = pd.crosstab(df_test["Label"], df_test["classification"], rownames=['Actual'], colnames=['Predicted'])
#print("Confusion Matrix for seal test set: \n")
#print(confusion_matrix)
#print("\nAccuracy on test set: %.3f \n" % accuracy_test)