Exemple #1
0
    while True:
        choice = input('\nChoice: ').upper()[0]
        if choice == 'Q':
            sys.exit(0)
        if choice not in choices:
            print('Invalid choice!')
        else:
            break
    chi = p_table[choices[choice]]
    tree = id3.id3_tree.grow_tree(training_data_file, chi)
    tree.dump_model(model_file)
    answers = []
    for row, class_ in util.load_test(test_data_file):
        values = {k:v for k,v in zip(tree.headers, row)}
        answers.append(tree.classify(values) == class_)
    accuracy = sum(answers) / len(answers)
    print('\nAccuracy: {:.2%}'.format(accuracy))
    
if __name__ == '__main__':
    if sys.version_info[0] != 3:
        sys.stderr.write('This program is written for Python 3.x. You are using Python {0}.{1}. '
                         'Please switch to Python 3.x and try again!\n'.format(*sys.version_info[:2]))
        sys.exit(1)
    args = sys.argv
    if len(args) != 4:
        sys.stderr.write('Proper usage:\n\n\tpython -m id3.main <training_input_file> <test_input_file> <model_output_file>\n\n')
        sys.exit(1)
    for f_name in args[1:3]:
        util.verify(f_name)
    main(*args[1:]) 
Exemple #2
0
def grow_tree(data_file, chi=False):
    util.verify(data_file)
    data = np.loadtxt(data_file, skiprows=1, delimiter=',').T
    with open(data_file) as f:
        headers = f.readline().strip().split(',')
    return ID3Tree(data, headers, chi=chi)