def classify_instance(boosted_tree, example): trueVotes = 0.0 falseVotes = 0.0 for model in boosted_tree: if dt.classify_sequence(model[0], example.sequence): trueVotes += 1.0 * log(1 / model[1]) else: falseVotes += 1.0 * log(1/ model[1]) return trueVotes >= falseVotes
def construct_boosted_tree(training): weights = map(lambda x: 1.0 / len(training), training) decisionTrees = [] for roundIdx in range(0, NUMBER_OF_ROUNDS): weights_sum = sum(weights) weights = map(lambda weight: weight / weights_sum, weights) tree = dt.construct_tree(weighted_bootstrap_set(training, weights)) error = 0.0 for i in range(0, len(training)): example = training[i] if example.promoter != dt.classify_sequence(tree, example.sequence): error += weights[i] if error > .5: return decisionTrees beta = error / (1 - error) for i in range(0, len(training)): example = training[i] if example.promoter == dt.classify_sequence(tree, example.sequence): weights[i] = weights[i] * beta decisionTrees.append((tree, beta)) return decisionTrees
def classify_instance(ensemble, example): trueVotes = 0 for model in ensemble: if dt.classify_sequence(model, example.sequence): trueVotes += 1 return trueVotes > len(ensemble) / 2