def fill_children(self, input_data): # On condition self.filter_feature, self.filter_values split with value self.me if self.answer >= 0 or self.me == -1: # Nothing further return if len(self.filter_feature) == len(input_data[0]): ans = self.restrict_data(input_data)[-1] # Answer at leaf self.me = ans return left = copy.deepcopy(self) # DO DEEP COPY !!! left.filter_feature.append(self.me) left.filter_values.append('low') left_data = left.restrict_data(input_data) if len(left_data) <= 0: # Left empty self.left_child = Node() else: left_h_to_x = learning.calc_h_to_x(left_data) if left_h_to_x == -1: # All no left.answer = 0 self.left_child = left elif left_h_to_x == -2: # All yes left.answer = 1 self.left_child = left else: left.me = learning.split_on(left_h_to_x, left_data, left.filter_feature) self.left_child = left # Left done middle = copy.deepcopy(self) # DO DEEP COPY !!! middle.filter_feature.append(self.me) middle.filter_values.append('med') middle_data = middle.restrict_data(input_data) if len(middle_data) <= 0: # print 'Mid empty' self.middle_child = Node() else: middle_h_to_x = learning.calc_h_to_x(middle_data) # print 'middle_h_to_x : ', middle_h_to_x if int(middle_h_to_x) == -1: # All no # print 'Mid all no' middle.answer = 0 self.middle_child = middle elif int(middle_h_to_x) == -2: # All yes # print 'Mid all yes' middle.answer = 1 self.middle_child = middle else: middle.me = learning.split_on(middle_h_to_x, middle_data, middle.filter_feature) self.middle_child = middle #print 'Middle done' right = copy.deepcopy(self) # DO DEEP COPY !!! right.filter_feature.append(self.me) right.filter_values.append('high') right_data = right.restrict_data(input_data) if len(right_data) <= 0: # print 'Right empty' self.right_child = Node() else: right_h_to_x = learning.calc_h_to_x(right_data) if right_h_to_x == -1: # All yes # print 'Right all yes' right.answer = 1 self.right_child = right elif right_h_to_x == -2: # All no # print 'Right all no' right.answer = 0 self.right_child = right else: right.me = learning.split_on(right_h_to_x, right_data, right.filter_feature) self.right_child = right #print 'Right done' # Left child : self.left_child.me, self.left_child.answer # Middle child : self.middle_child.me, self.middle_child.answer # Right child : self.right_child.me, self.right_child.answer #print "---------------------------------------------------" self.left_child.fill_children(input_data) self.middle_child.fill_children(input_data) self.right_child.fill_children(input_data) return
def fill_me(self, input_data): h_of_x = learning.calc_h_to_x(input_data) self.me = learning.split_on(h_of_x, input_data, self.filter_feature) return