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
final_accuracy = (total_accuracy / total_elements) return final_accuracy if __name__ == '__main__': data, filename = process_file() print("This is a decision tree classifier program") print("\nThe dataset you've chosen is {}".format(filename)) print("\nIf you wanted to experiment with a different dataset,") print("please quit this program and enter:") print("`python driver.py data_file.txt`") print("You can choose from any files under the 'dataset/' directory.") t = DecisionTree(data) t.build_tree() print("\n\n...Successfully built a classifer based on the datset") print("\nIf you want to print the tree, hit 1. Otherwise, hit `enter`:\n") see_tree = input() if see_tree == '1': print("Classification Tree from {} data".format(filename)) print("=============================================================") t.print_tree() print("=============================================================") 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.")