-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytree.py
179 lines (141 loc) · 4.7 KB
/
mytree.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
import math
import sys
import pandas as pd
import numpy as np
import csv
# My version of an ID3 decision tree
if len(sys.argv) != 3:
print(" Incorrect number of arguments! (Expected: 2, Actual: " + str(len(sys.argv) - 1) + ")")
exit(0)
training = pd.read_csv(sys.argv[1])
# training = pd.read_csv("data.csv")
test = pd.read_csv(sys.argv[2])
def safe_fraction(numerator, denominator):
if denominator == 0:
return 0
return numerator / denominator
def safe_log(num):
if num == 0:
return 0
return math.log(num, 2)
def increment(num):
num += 1
return num
def key_entropy(df):
entropy = 0
last = df.keys()[-1]
values = df[last].unique()
for value in values:
# print(df[last].value_counts()[value])
fraction = safe_fraction(df[last].value_counts()[value], len(training[last]))
entropy -= fraction * safe_log(fraction)
return entropy
def attr_entropy(df, attribute):
last = df.keys()[-1]
target_vars = df[last].unique()
attr_vars = df[attribute].unique()
entropy = 0
for attr_var in attr_vars:
sub_entropy = 0
denominator = 0
for target_var in target_vars:
numerator = len(df[attribute][df[attribute] == attr_var][df[last] == target_var])
denominator = len(df[attribute][df[attribute] == attr_var])
fraction = safe_fraction(numerator, denominator)
sub_entropy -= fraction * safe_log(fraction)
fraction = safe_fraction(denominator, len(df))
entropy -= fraction * sub_entropy
return entropy
def information_gain(df, attribute):
return key_entropy(df) + attr_entropy(df, attribute)
def information_gains(df):
ig_arr = []
for column in df:
if column == df.keys()[-1]:
continue
ig_arr.append(information_gain(df, column))
return ig_arr
def pick_best(df, ig_arr):
return str(df.columns[ig_arr.index(max(ig_arr))])
def get_subtable(df, node, value):
return df[df[node] == value].reset_index(drop=True)
def predict(tree, data):
if tree.is_last is True:
return tree.attr
through = False
for child in tree.children:
if str(data[tree.attr]) == child.path:
# print("yes ", child.path)
through = True
return predict(child, data)
if through is False and tree.children[0].path.isdigit():
closest = float(math.inf)
send = tree.children[0]
for child in tree.children:
temp = abs(int(child.path) - int(data[tree.attr]))
if temp < closest:
closest = temp
send = child
return predict(send, data)
else:
return '-'
class Node:
def __init__(self, attr=""):
self.children = []
self.attr = attr
self.path: str = ""
self.df = None
self.is_last = False
def __repr__(self, level=0):
if self.path == '':
ret = "\t" * level + repr(self.attr) + "\n"
else:
ret = "\t" * level + repr(self.path) + " " + repr(self.attr) + "\n"
for child in self.children:
ret += child.__repr__(level + 1)
return ret
def build_tree(self, df, depth, limit, path=""):
self.df = df
self.path = str(path)
if depth == limit:
self.is_last = True
last = df.keys()[-1]
plus = df[last].value_counts()['+']
minus = df[last].value_counts()['-']
if plus > minus:
self.attr = '+'
else:
self.attr = '-'
return
if len(df.columns) == 1:
return
self.attr = pick_best(df, information_gains(df))
attr_vars = np.unique(df[self.attr])
# print(self.data, attr_vars)
df_arr = []
for attr_var in attr_vars:
df_arr.append(get_subtable(df, self.attr, attr_var))
if not df_arr:
return
i = 0
for sub_df in df_arr:
last = sub_df.keys()[-1]
if len(np.unique(sub_df[last])) != 1:
self.children.append(Node())
self.children[-1].build_tree(sub_df, increment(depth), limit, attr_vars[i])
i += 1
else:
end = sub_df[last][0]
self.children.append(Node(end))
self.children[-1].path = str(attr_vars[i])
self.children[-1].is_last = True
i += 1
root = Node()
max_depth = 3
root.build_tree(training, 0, max_depth)
print(root)
print("Testing with max depth of", max_depth)
file = open('output.txt', 'w')
for index, row in test.iterrows():
# print(index, predict(root, row))
print(predict(root, row), file=file)