Resolve split_value issue in DTLearner and pass all tests.
This commit is contained in:
@@ -1,18 +1,14 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
from AbstractTreeLearner import AbstractTreeLearner
|
||||||
|
|
||||||
|
|
||||||
class BagLearner(object):
|
class BagLearner(AbstractTreeLearner):
|
||||||
def __init__(self, learner, bags=20, boost=False, verbose=False, **kwargs):
|
def __init__(self, learner, bags=9, boost=False, verbose=False, kwargs={}):
|
||||||
self.learner = learner
|
self.learner = learner
|
||||||
self.bags = bags
|
|
||||||
self.boost = boost
|
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
self.kwargs = kwargs
|
self.bags = bags
|
||||||
self.learners = [learner(**kwargs) for _ in range(bags)]
|
self.learners = [learner(**kwargs) for _ in range(bags)]
|
||||||
|
|
||||||
def author(self):
|
|
||||||
return 'felixm' # replace tb34 with your Georgia Tech username
|
|
||||||
|
|
||||||
def get_bag(self, data_x, data_y):
|
def get_bag(self, data_x, data_y):
|
||||||
num_items = int(data_x.shape[0] * 0.5) # 50% of samples
|
num_items = int(data_x.shape[0] * 0.5) # 50% of samples
|
||||||
bag_x, bag_y = [], []
|
bag_x, bag_y = [], []
|
||||||
@@ -22,7 +18,6 @@ class BagLearner(object):
|
|||||||
bag_y.append(data_y[i])
|
bag_y.append(data_y[i])
|
||||||
return np.array(bag_x), np.array(bag_y)
|
return np.array(bag_x), np.array(bag_y)
|
||||||
|
|
||||||
|
|
||||||
def addEvidence(self, data_x, data_y):
|
def addEvidence(self, data_x, data_y):
|
||||||
"""
|
"""
|
||||||
@summary: Add training data to learner
|
@summary: Add training data to learner
|
||||||
@@ -36,10 +31,8 @@ class BagLearner(object):
|
|||||||
def query(self, points):
|
def query(self, points):
|
||||||
"""
|
"""
|
||||||
@summary: Estimate a set of test points given the model we built.
|
@summary: Estimate a set of test points given the model we built.
|
||||||
@param points: should be a numpy array with each row corresponding to a specific query.
|
@param points: numpy array with each row corresponding to a query.
|
||||||
@returns the estimated values according to the saved model.
|
@returns the estimated values according to the saved model.
|
||||||
"""
|
"""
|
||||||
return np.mean([l.query(points) for l in self.learners], axis=0)
|
return np.mean([l.query(points) for l in self.learners], axis=0)
|
||||||
|
|
||||||
if __name__=="__main__":
|
|
||||||
print("the secret clue is 'zzyzx'")
|
|
||||||
|
|||||||
@@ -8,9 +8,6 @@ class DTLearner(AbstractTreeLearner):
|
|||||||
self.leaf_size = leaf_size
|
self.leaf_size = leaf_size
|
||||||
self.verbose = verbose
|
self.verbose = verbose
|
||||||
|
|
||||||
def author(self):
|
|
||||||
return 'felixm' # replace tb34 with your Georgia Tech username
|
|
||||||
|
|
||||||
def get_correlations(self, xs, y):
|
def get_correlations(self, xs, y):
|
||||||
""" Return a list of sorted 2-tuples where the first element
|
""" Return a list of sorted 2-tuples where the first element
|
||||||
is the correlation and the second element is the index. Sorted by
|
is the correlation and the second element is the index. Sorted by
|
||||||
@@ -25,14 +22,23 @@ class DTLearner(AbstractTreeLearner):
|
|||||||
return correlations
|
return correlations
|
||||||
|
|
||||||
def get_i_and_split_value(self, xs, y):
|
def get_i_and_split_value(self, xs, y):
|
||||||
|
# If all elements are true we would get one sub-tree with zero
|
||||||
|
# elements, but we need at least one element in both trees. We avoid
|
||||||
|
# zero-trees in two steps. First we take the average between the median
|
||||||
|
# value and a smaller value an use that as the new split value. If that
|
||||||
|
# doesn't work (when all values are the same) we choose the X with the
|
||||||
|
# next smaller correlation. We assert that not all values are
|
||||||
|
# smaller/equal to the split value at the end.
|
||||||
for _, i in self.get_correlations(xs, y):
|
for _, i in self.get_correlations(xs, y):
|
||||||
split_value = np.median(xs[:,i])
|
split_value = np.median(xs[:,i])
|
||||||
select = xs[:, i] <= split_value
|
select = xs[:, i] <= split_value
|
||||||
# If all elements are true we would get one sub-tree with zero
|
if select.all():
|
||||||
# elements, but we need at least one element. Therefore, we only
|
for value in xs[:, i]:
|
||||||
# choose the index if not all elements are true. If they are we go
|
if value < split_value:
|
||||||
# to the next smaller correlation.
|
split_value = (value + split_value) / 2.0
|
||||||
|
select = xs[:, i] <= split_value
|
||||||
if not select.all():
|
if not select.all():
|
||||||
break
|
break
|
||||||
|
assert(not select.all())
|
||||||
return i, split_value
|
return i, split_value
|
||||||
|
|
||||||
|
|||||||
@@ -48,12 +48,6 @@ if __name__=="__main__":
|
|||||||
trainY = data[:train_rows,-1]
|
trainY = data[:train_rows,-1]
|
||||||
testX = data[train_rows:,0:-1]
|
testX = data[train_rows:,0:-1]
|
||||||
testY = data[train_rows:,-1]
|
testY = data[train_rows:,-1]
|
||||||
|
|
||||||
# trainX = data[:, 0:-1]
|
|
||||||
# trainY = data[:, -1]
|
|
||||||
# testX = data[:, 0:-1]
|
|
||||||
# testY = data[:, -1]
|
|
||||||
|
|
||||||
print(f"{testX.shape}")
|
print(f"{testX.shape}")
|
||||||
print(f"{testY.shape}")
|
print(f"{testY.shape}")
|
||||||
|
|
||||||
@@ -85,8 +79,7 @@ if __name__=="__main__":
|
|||||||
|
|
||||||
# test_learner(lrl.LinRegLearner)
|
# test_learner(lrl.LinRegLearner)
|
||||||
test_learner(dtl.DTLearner, leaf_size=1)
|
test_learner(dtl.DTLearner, leaf_size=1)
|
||||||
test_learner(rtl.RTLearner)
|
# test_learner(rtl.RTLearner, leaf_size=6)
|
||||||
test_learner(rtl.RTLearner, leaf_size=5)
|
test_learner(bgl.BagLearner, learner=dtl.DTLearner, bags=20, kwargs = {'leaf_size': 5})
|
||||||
# test_learner(bgl.BagLearner, learner=dtl.DTLearner, bags=20)
|
test_learner(isl.InsaneLearner)
|
||||||
# learner = isl.InsaneLearner()
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user