Example #1
0
    def get_node(self, node_id):
        c = self.conn.cursor()
        c.execute("SELECT str_id, host_ip, connected,\
                          last_conn_time, node_data\
                   FROM nodes\
                   WHERE str_id=%s", (node_id,))
        row = c.fetchone()
        if not row:
            return None

        str_id, host_ip, connected, last_conn_time, node_data = row[0]
        node = Node(str_id)
        node.ip_addr = host_ip
        node.connected = connected
        node.last_conn_time = last_conn_time
        if node_data:
            node.load_data(node_data)

        return node
Example #2
0
class Tree():
    '''Class for a binary decision tree'''
    def __init__(self, tree_id, max_depth, impurity, ndims, min_leaf, num_tests,
                 num_classes, decision_fn=hardSplit):
        self.tree_id = tree_id
        self.max_depth = max_depth
        self.impurity = impurity
        self.ndims = ndims
        self.min_leaf = min_leaf
        self.num_tests = num_tests
        self.num_classes = num_classes
        self.decision_fn = decision_fn
        # Create root node
        dct = {'node_id' : 0,
               'residual_depth' : self.max_depth,
               'parent' : None,
               'impurity' : impurity,
               'min_leaf' : min_leaf,
               'ndims' : ndims,
               'num_tests' : num_tests,
               'num_classes' : num_classes,
               'decision_fn' : decision_fn}
        self.root = Node(**dct)
    
    def train(self, X, y):
        '''Build a randomTree'''
        self.root.load_data(X, y)
        done = False
        node = self.root
        N = node.train()
        return N
    
    def apply(self, X):
        '''Propagate the data through the tree'''
        marker = numpy.arange(X.shape[0])
        result = numpy.asarray(self.root.apply(X, marker))
        result = result[numpy.argsort(result[:,0]),1]
        return result