-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdt.py
185 lines (146 loc) · 6.72 KB
/
dt.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
"""
In dt.py, you will implement a basic decision tree classifier for
binary classification. Your implementation should be based on the
minimum classification error heuristic (even though this isn't ideal,
it's easier to code than the information-based metrics).
"""
from numpy import *
import datasets
from binary import *
import util
class DT(BinaryClassifier):
"""
This class defines the decision tree implementation. It comes
with a partial implementation for the tree data structure that
will enable us to print the tree in a canonical form.
"""
def __init__(self, opts):
"""
Initialize our internal state. The options are:
opts.maxDepth = maximum number of features to split on
(i.e., if maxDepth == 1, then we're a stump)
"""
self.opts = opts
# initialize the tree data structure. all tree nodes have a
# "isLeaf" field that is true for leaves and false otherwise.
# leaves have an assigned class (+1 or -1). internal nodes
# have a feature to split on, a left child (for when the
# feature value is < 0.5) and a right child (for when the
# feature value is >= 0.5)
self.isLeaf = True
self.label = 1
def online(self):
"""
Our decision trees are batch
"""
return False
def __repr__(self):
"""
Return a string representation of the tree
"""
return self.displayTree(0)
def displayTree(self, depth):
# recursively display a tree
if self.isLeaf:
return (" " * (depth*2)) + "Leaf " + str(self.label) + "\n"
else:
return (" " * (depth*2)) + "Branch " + repr(datasets.SentimentData.words[self.feature]) + ", " + str(self.feature) + "\n" + \
self.left.displayTree(depth+1) + \
self.right.displayTree(depth+1)
def predict(self, X):
"""
Traverse the tree to make predictions. You should threshold X
at 0.5, so <0.5 means left branch and >=0.5 means right
branch.
"""
### TODO: YOUR CODE HERE
if self.isLeaf:
return str(self.label)
else:
if X[self.feature] < 0.5:
return self.left.predict(X)
else:
return self.right.predict(X)
def trainDT(self, X, Y, maxDepth, used):
"""
recursively build the decision tree
"""
# get the size of the data set
N,D = X.shape
# check to see if we're either out of depth or no longer
# have any decisions to make
if maxDepth <= 0 or len(util.uniq(Y)) <= 1:
# we'd better end at this point. need to figure
# out the label to return
self.isLeaf = True # TODO: well, that's leaf
self.label = util.mode(Y) # TODO: and retturn mode of labels
else:
# we need to find a feature to split on
bestFeature = -1 # which feature has lowest error
bestError = N # the number of errors for this feature
for d in range(D):
# have we used this feature yet
if d in used:
continue
# suppose we split on this feature; what labels
# would go left and right?
leftY = Y[X[:, d] < 0.5] # TODO: Labels which feature value less than .5
rightY = Y[X[:, d] >= 0.5] # TODO: Labels which feature value greater than or equal to .5
# we'll classify the left points as their most
# common class and ditto right points. our error
# is the how many are not their mode.
error = size(nonzero([leftY != util.mode(leftY)])) + size(nonzero([rightY != util.mode(rightY)])) # TODO: counting in each branch amount of labels that are not equal to their mode
# check to see if this is a better error rate
if error <= bestError:
bestFeature = d
bestError = error
if bestFeature < 0:
# this shouldn't happen, but just in case...
self.isLeaf = True
self.label = util.mode(Y)
else:
self.isLeaf = False # TODO: that's not leaf, cause it is a whole branch
self.feature = bestFeature # TODO: which carries its own feature
self.left = DT({'maxDepth': maxDepth-1})
self.right = DT({'maxDepth': maxDepth-1})
# recurse on our children by calling
# self.left.trainDT(...)
# and
# self.right.trainDT(...)
# with appropriate arguments
# TODO: define X and Y for left and right parts of current tree and init DT training
# redefine labels with the best feature
used = used + [self.feature] # anti infinite loop
leftX = X[X[:, self.feature] < 0.5]
rightX = X[X[:, self.feature] >= 0.5]
leftY = Y[X[:, self.feature] < 0.5]
rightY = Y[X[:, self.feature] >= 0.5]
self.left.trainDT(leftX, leftY, maxDepth-1, used)
self.right.trainDT(rightX, rightY, maxDepth-1, used)
def train(self, X, Y):
"""
Build a decision tree based on the data from X and Y. X is a
matrix (N x D) for N many examples on D features. Y is an
N-length vector of +1/-1 entries.
Some hints/suggestions:
- make sure you don't build the tree deeper than self.opts['maxDepth']
- make sure you don't try to reuse features (this could lead
to very deep trees that keep splitting on the same feature
over and over again)
- it is very useful to be able to 'split' matrices and vectors:
if you want the ids for all the Xs for which the 5th feature is
on, say X(:,5)>=0.5. If you want the corresponding classes,
say Y(X(:,5)>=0.5) and if you want the corresponding rows of X,
say X(X(:,5)>=0.5,:)
- i suggest having train() just call a second function that
takes additional arguments telling us how much more depth we
have left and what features we've used already
- take a look at the 'mode' and 'uniq' functions in util.py
"""
self.trainDT(X, Y, self.opts['maxDepth'], [])
def getRepresentation(self):
"""
Return our internal representation: for DTs, this is just our
tree structure -- i.e., ourselves
"""
return self