def pruning_decision_tree_test():
    # load data
    X_train, y_train, X_test, y_test = data.sample_decision_tree_pruning()

    # build the tree
    dTree = decision_tree.DecisionTree()
    dTree.train(X_train, y_train)

    # print
    print('Your decision tree:')
    Utils.print_tree(dTree)
    print('My decision tree:')
    print(
        'branch 0{\n\tdeep: 0\n\tnum of samples for each class: 5 : 9 \n\tsplit by dim 0\n\tbranch 0->0{\n\t\tdeep: 1'
        '\n\t\tnum of samples for each class: 3 : 2 \n\t\tsplit by dim 1\n\t\tbranch 0->0->0{\n\t\t\tdeep: 2\n\t\t\t'
        'num of samples for each class: 3 \n\t\t\tclass:0\n\t\t}\n\t\tbranch 0->0->1{\n\t\t\tdeep: 2\n\t\t\tnum of '
        'samples for each class: 2 \n\t\t\tclass:1\n\t\t}\n\t}\n\tbranch 0->1{\n\t\tdeep: 1\n\t\tnum of samples for '
        'each class: 4 \n\t\tclass:1\n\t}\n\tbranch 0->2{\n\t\tdeep: 1\n\t\tnum of samples for each class: 2 : 3 '
        '\n\t\tsplit by dim 2\n\t\tbranch 0->2->0{\n\t\t\tdeep: 2\n\t\t\tnum of samples for each class: 3 \n\t\t\t'
        'class:1\n\t\t}\n\t\tbranch 0->2->1{\n\t\t\tdeep: 2\n\t\t\tnum of samples for each class: 2 \n\t\t\tclass:0'
        '\n\t\t}\n\t}\n}')

    Utils.reduced_error_prunning(dTree, X_test, y_test)

    print('Your decision tree after pruning:')
    Utils.print_tree(dTree)
    print('My decision tree after pruning:')
    print(
        'branch 0{\n\tdeep: 0\n\tnum of samples for each class: 5 : 9 \n\tsplit by dim 0\n\tbranch 0->0{\n\t\tdeep: '
        '1\n\t\tnum of samples for each class: 3 : 2 \n\t\tsplit by dim 1\n\t\tbranch 0->0->0{\n\t\t\tdeep: 2\n\t\t\t'
        'num of samples for each class: 3 \n\t\t\tclass:0\n\t\t}\n\t\tbranch 0->0->1{\n\t\t\tdeep: 2\n\t\t\tnum of '
        'samples for each class: 2 \n\t\t\tclass:1\n\t\t}\n\t}\n\tbranch 0->1{\n\t\tdeep: 1\n\t\tnum of samples for '
        'each class: 4 \n\t\tclass:1\n\t}\n\tbranch 0->2{\n\t\tdeep: 1\n\t\tnum of samples for each class: 2 : 3 '
        '\n\t\tclass:1\n\t}\n}')
Ejemplo n.º 2
0
def test_big_tree():
    # load data
    X_train, y_train, X_test, y_test = data.load_decision_tree_data()

    # set classifier
    dTree = decision_tree.DecisionTree()

    # training
    dTree.train(X_train.tolist(), y_train.tolist())

    # print
    # Utils.print_tree(dTree)

    # testing
    y_est_test = dTree.predict(X_test)
    test_accu = accuracy_score(y_est_test, y_test)
    print('test_accu', test_accu)

    Utils.reduced_error_prunning(dTree, X_test, y_test)

    y_est_test = dTree.predict(X_test)
    test_accu = accuracy_score(y_est_test, y_test)
    print('test_accu', test_accu)

    # print
    Utils.print_tree(dTree)
Ejemplo n.º 3
0
def t2():
    data = np.loadtxt('car.data',delimiter=',')
    x_train = pd.DataFrame(data)
    y_train = x_train[0].tolist()
    x_train = x_train.drop([0],axis=1)
    x_train = np.array(x_train).tolist()
    x_test = x_train[1500:]
    y_test = y_train[1500:]
    x_train = x_train[:1500]
    y_train = y_train[:1500]

    tree = DecisionTree()
    tree.train(x_train,y_train)
    p = tree.predict(x_train)
    U.print_tree(decisionTree=tree)
    U.reduced_error_prunning(decisionTree=tree,X_test=x_test,y_test=y_test)
    print('---------------------------')
    U.print_tree(decisionTree=tree)
Ejemplo n.º 4
0
def test_tree():
    features, labels = data.sample_decision_tree_data()
    # build the tree
    dTree = decision_tree.DecisionTree()
    dTree.train(features, labels)
    # print
    Utils.print_tree(dTree)

    # data
    X_test, y_test = data.sample_decision_tree_test()
    # testing
    y_est_test = dTree.predict(X_test)
    test_accu = accuracy_score(y_est_test, y_test)
    print('test_accu', test_accu)

    Utils.reduced_error_prunning(dTree, X_test, y_test)

    y_est_test = dTree.predict(X_test)
    test_accu = accuracy_score(y_est_test, y_test)
    print('test_accu', test_accu)
Ejemplo n.º 5
0
"""
#load data
X_train, y_train, X_test, y_test = data.load_decision_tree_data()

# set classifier
dTree = decision_tree.DecisionTree()

# training
dTree.train(X_train.tolist(), y_train.tolist())

# print
Utils.print_tree(dTree)

import json
# testing
y_est_test = dTree.predict(X_test)
test_accu = accuracy_score(y_est_test, y_test)
print("Before Pruning:")
print('test_accu', test_accu)
print('self-calc-acc', Utils.get_accuracy(y_est_test, y_test))
print()

Utils.reduced_error_prunning(dTree, X_test, y_test)

y_est_test = dTree.predict(X_test)

print()
print("After Pruning:")
test_accu = accuracy_score(y_est_test, y_test)
print('test_accu', test_accu)
print('self-calc-acc', Utils.get_accuracy(y_est_test, y_test))