How to use the dt.ID3 function in dt

To help you get started, we’ve selected a few dt examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mazefeng / ml / dt.py View on Github external
if y_pred == None:
                y_pred = node['LABEL']
            if y_pred == y:
                correct += 1

        return 1.0 * correct / len(Y)

if __name__ == '__main__':
   
    train_path = 'data/2_newsgroups.train'
    test_path = 'data/2_newsgroups.test'

    X_train, Y_train = read_sparse_data(open(train_path))
    X_test, Y_test = read_sparse_data(open(test_path))

    clf = ID3()
    clf.train(X_train, Y_train)
    acc_train = clf.test(X_train, Y_train)
    acc_test = clf.test(X_test, Y_test)

    print >> sys.stderr, 'Training accuracy for ID3 : %f%%' % (100 * acc_train)
    print >> sys.stderr, 'Test accuracy for ID3 : %f%%' % (100 * acc_test)
   
    clf.dump_model(open('data/dt.model', 'w'), open('data/dt.rule_set', 'w'))