示例#1
0
def accuracy_test(rows):
    """Performs cross-out-one validation methods on the dataset passed."""
    # Removing the label
    rows = rows[1:]

    total_accuracy = 0.0
    total_elements = len(rows)

    for i, row in enumerate(rows):
        singled_out = row
        rows.remove(row)
        remaining_rows = rows
        actual_label = row[-1]

        dt = DecisionTree(remaining_rows)
        node = dt.build_tree()
        #node = build_tree(remaining_rows)
        result = dt.classify(singled_out, node)

        # either 'yes' or 'no' prediction
        if len(result) == 1:
            if actual_label in result:
                total_accuracy += 1
                continue
            else:
                total_accuracy += 0
                continue
        # both 'yes' and 'no' prediction
        else:
            prob_correct = result[actual_label]
            if actual_label == 'yes':
                incorrect_label = 'no'
            else:
                incorrect_label = 'yes'
            prob_incorrect = result[incorrect_label]

            total_prob = prob_correct + prob_incorrect
            accuracy_for_this_test = prob_correct / total_prob

        total_accuracy += accuracy_for_this_test

        rows.append(singled_out)

    final_accuracy = (total_accuracy / total_elements)

    return final_accuracy
示例#2
0
    print(
        "You can randomly choose a data point, and this program can classify")
    print(
        "that data point for you. Hit 1 if you want to test a random example")
    print("Hit any key to skip.")
    will_rand = input()

    while True:
        if will_rand != '1':
            break

        chosen = random.randint(1, len(data) - 1)
        print("\nExample we will classify ", data[chosen])
        actual = data[chosen][-1]
        print("Actual label on this example is **{}**".format(actual))
        c = t.classify(row=data[chosen])
        print("The prediction based on the classifier is: ")
        print("Prediction: confidence % =", t._prediction(c))

        print(
            "\n\nEnter 1 if you want to play again. Hit `enter` if you are done."
        )

        play_again = input()
        if play_again != '1':
            break

    print("To check the accuracy test using cross validation, hit 1")
    print("Hit `enter` to skip accuracy testing.")
    acc_test = input()
    if acc_test == '1':