Continue work on project 3.
This commit is contained in:
@@ -1,28 +1,51 @@
|
||||
import numpy as np
|
||||
|
||||
|
||||
class DTLearner(object):
|
||||
|
||||
LEAF = -1
|
||||
NA = -1
|
||||
|
||||
def __init__(self, leaf_size = 1, verbose = False):
|
||||
pass # move along, these aren't the drones you're looking for
|
||||
self.leaf_size = leaf_size
|
||||
self.verbose = verbose
|
||||
|
||||
def author(self):
|
||||
return 'felixm' # replace tb34 with your Georgia Tech username
|
||||
|
||||
def addEvidence(self, dataX, dataY):
|
||||
def create_node(self, factor: int, split: int, left: int, right: int):
|
||||
return np.array((factor, split, left, right))
|
||||
|
||||
|
||||
def build_tree(self, xs, y):
|
||||
assert(xs.shape[0] == y.shape[0])
|
||||
assert(xs.shape[0] > 0) # If this is 0 something went wrong.
|
||||
|
||||
if xs.shape[0] == 1:
|
||||
return self.create_node(self.LEAF, y[0], self.NA, self.NA)
|
||||
|
||||
if np.all(y[0] == y):
|
||||
return self.create_node(self.LEAV, y[0], self.NA, self.NA)
|
||||
|
||||
# XXX: continue here
|
||||
y = np.array([y])
|
||||
correlations = np.corrcoef(xs, y, rowvar=True)
|
||||
print(f"{correlations=}")
|
||||
|
||||
return 0
|
||||
|
||||
def addEvidence(self, data_x, data_y):
|
||||
"""
|
||||
@summary: Add training data to learner
|
||||
@param dataX: X values of data to add
|
||||
@param dataY: the Y training values
|
||||
"""
|
||||
if self.verbose:
|
||||
print(data_x)
|
||||
print(data_y)
|
||||
self.tree = self.build_tree(data_x, data_y)
|
||||
|
||||
# slap on 1s column so linear regression finds a constant term
|
||||
newdataX = np.ones([dataX.shape[0], dataX.shape[1]+1])
|
||||
newdataX[:,0:dataX.shape[1]] = dataX
|
||||
|
||||
# build and save the model
|
||||
self.model_coefs, residuals, rank, s = np.linalg.lstsq(newdataX,
|
||||
dataY,
|
||||
rcond=None)
|
||||
|
||||
def query(self,points):
|
||||
"""
|
||||
@@ -30,7 +53,8 @@ class DTLearner(object):
|
||||
@param points: should be a numpy array with each row corresponding to a specific query.
|
||||
@returns the estimated values according to the saved model.
|
||||
"""
|
||||
return (self.model_coefs[:-1] * points).sum(axis = 1) + self.model_coefs[-1]
|
||||
return
|
||||
# return (self.model_coefs[:-1] * points).sum(axis = 1) + self.model_coefs[-1]
|
||||
|
||||
if __name__=="__main__":
|
||||
print("the secret clue is 'zzyzx'")
|
||||
|
||||
Reference in New Issue
Block a user