def run_bagged_forest_prediction(examples_bt, test_examples_bt):

	num_correct = 0
	print("accuracy for bagged trees: training")
	for i, example in enumerate(examples_bt):
		print(i)
		decision = 0
		for dtree in dtrees:
			decision += use_tree(dtree, example)
		if decision > 0 and example.label == 1:
			num_correct += 1
		elif decision <= 0 and example.label == -1:
			num_correct += 1

	print(num_correct / len(examples_bt))

	num_correct = 0
	print("accuracy for bagged trees: test")
	for example in test_examples_bt:
		decision = 0
		for dtree in dtrees:
			decision += use_tree(dtree, example)
		if decision > 0 and example.label == 1:
			num_correct += 1
		elif decision <= 0 and example.label == -1:
			num_correct += 1

	print(num_correct / len(test_examples_bt))
def transform_trees_to_feats(examples, trees):
	example_list = list(examples)
	new_list = []
	for example in example_list:
		feat_array = np.array([use_tree(tree, example) for tree in trees])
		new_list.append(LabeledEx(example.label, feat_array))
	return new_list
Пример #3
0
def calc_error_target(stump, weights, examples, target_label):
    error = 0
    right_or_wrong = []
    for i, ex in enumerate(examples):
        guess = use_tree(stump, ex)
        if guess != ex.label and guess != -1:
            error += weights[i]
            right_or_wrong.append(0)
        elif guess == -1 and ex.label == target_label:
            error += weights[i]
            right_or_wrong.append(0)
        else:
            right_or_wrong.append(1)

    return error, right_or_wrong
Пример #4
0
def use_classifier_target(H, examples, target_label):

    num_correct = 0
    if len(H) == 0:
        return 0
    for ex in examples:
        sign = 0
        for (alpha, h) in H:

            guess = use_tree(h, ex)
            if guess == target_label:
                sign += alpha
            else:
                sign -= alpha

        if sign >= 0 and ex.label == target_label:
            num_correct += 1
        elif sign < 0 and ex.label != target_label:
            num_correct += 1

    return num_correct
Пример #5
0
def use_classifier_multiclass(H, examples, labels):

    num_correct = 0
    for ex in examples:
        best = [-1000, None]
        for label in labels:
            sign = 0
            for (alpha, h, h_label) in H:
                guess = use_tree(h, ex)
                if guess == h_label:
                    sign += alpha
                else:
                    sign -= alpha

            if sign > best[0]:
                best[0] = sign
                best[1] = label

        if best[1] == ex.label:
            num_correct += 1

    return num_correct
def transform_trees_to_feats(examples, trees):
    example_list = list(examples)
    for example in example_list:
        example.feats = np.array([use_tree(tree, example) for tree in trees])
    return example_list