Ejemplo n.º 1
0
def test_decision_tree_classifier_predict():
    # Interview DataSet

    # Create X_train, y_train, X_test, and actuals
    X_train = []
    y_train = []
    X_test = [["Junior", "R", "yes", "no"], ["Junior", "Python", "no", "yes"],
              ["Senior", "Java", "no", "no", "False"]]
    actuals = ["True", "True", "False"]
    # Append the header
    X_train.append(interview_header)
    # Delete the classifier
    del X_train[0][-1]
    # Get X_train2
    for row in range(len(interview_table)):
        tmp = []
        for col in range(len(interview_table[0]) - 1):
            tmp.append(interview_table[row][col])
        X_train.append(tmp)
    # Get y_train2
    for row in range(len(interview_table)):
        y_train.append(interview_table[row][-1])
    # Create a MyDecisionTreeClassifier object
    test_predict = MyDecisionTreeClassifier()
    # Call fit
    test_predict.fit(X_train, y_train)
    # Call predict
    predicted = test_predict.predict(X_test)
    # Test
    assert predicted == actuals

    # Degrees dataset

    # Create X_train2, y_train2, X_test2, and actuals2
    X_train2 = []
    y_train2 = []
    X_test2 = [["A", "B", "A", "B", "B"], ["B", "A", "A", "B", "B"],
               ["A", "A", "B", "A", "A"]]
    actuals2 = ["SECOND", "SECOND", "FIRST"]
    # Append the header
    X_train2.append(degrees_header)
    # Delete the classifier
    del X_train2[0][-1]
    # Get X_train2
    for row in range(len(degrees_table)):
        tmp = []
        for col in range(len(degrees_table[0]) - 1):
            tmp.append(degrees_table[row][col])
        X_train2.append(tmp)
    # Get y_train2
    for row in range(len(degrees_table)):
        y_train2.append(degrees_table[row][-1])
    # Create a MyDecisionTreeClassifier object
    test_predict2 = MyDecisionTreeClassifier()
    # Call fit
    test_predict2.fit(X_train2, y_train2)
    # Call predict
    predicted2 = test_predict2.predict(X_test2)
    # Test
    assert predicted2 == actuals2
Ejemplo n.º 2
0
def test_decision_tree_classifier_predict():
    # Test the predict for the Interview Dataset
    myDecisionTreeClassifier = MyDecisionTreeClassifier()
    y_train, X_train = [], []
    for inst in interview_table:
        y_train.append(inst[-1])
        X_train.append(inst[:-1])
    myDecisionTreeClassifier.fit(X_train, y_train)

    X_test = [["Junior", "Java", "yes", "no"],
              ["Junior", "Java", "yes", "yes"]]
    y_pred = myDecisionTreeClassifier.predict(X_test)

    assert y_pred == ["True", "False"]

    # Test the result from Bramer
    y_train, X_train = [], []
    for inst in degrees_table:
        y_train.append(inst[-1])
        X_train.append(inst[:-1])
    myDecisionTreeClassifier.fit(X_train, y_train)

    X_test = [["B", "B", "B", "B", "B"], ["A", "A", "A", "A", "A"],
              ["A", "A", "A", "A", "B"]]
    y_pred = myDecisionTreeClassifier.predict(X_test)

    assert y_pred == ["SECOND", "FIRST", "FIRST"]
Ejemplo n.º 3
0
def random_forest_generation(remainder_set, N, M):
    complete_forest = []
    accuracy_forest = []
    pruned_forest = []
    for _ in range(N):
        training, validation = compute_bootstrapped_sample(remainder_set)
        X_train, y_train = split_x_y_train(training)
        X_test, y_test = split_x_y_train(validation)
        myDT = MyDecisionTreeClassifier()
        myDT.fit(X_train, y_train)
        y_predict = myDT.predict(X_test)
        accuracy = calculate_accuracy(y_predict, y_test)
        accuracy_forest.append(accuracy)
        complete_forest.append(myDT)
    sorted_forest = sort_forest(accuracy_forest, complete_forest)
    pruned_forest = prune_forest(sorted_forest, M)

    return pruned_forest
Ejemplo n.º 4
0
def test_decision_tree_classifier_fit():
    # Interview DataSet

    # Create X_train and y_train
    X_train = []
    y_train = []
    # Append the header
    X_train.append(interview_header)
    # Delete the classifier
    del X_train[0][-1]
    # Get X_train
    for row in range(len(interview_table)):
        tmp = []
        for col in range(len(interview_table[0]) - 1):
            tmp.append(interview_table[row][col])
        X_train.append(tmp)
    # Get y_train
    for row in range(len(interview_table)):
        y_train.append(interview_table[row][-1])
    # Create a MyDecisionTreeClassifier object
    test_fit = MyDecisionTreeClassifier()
    # Call fit
    test_fit.fit(X_train, y_train)
    # Test
    assert interview_tree == test_fit.tree

    # Degrees dataset

    # Create X_train2 and y_train2
    X_train2 = []
    y_train2 = []
    # Append the header
    X_train2.append(degrees_header)
    # Delete the classifier
    del X_train2[0][-1]
    # Get X_train
    for row in range(len(degrees_table)):
        tmp = []
        for col in range(len(degrees_table[0]) - 1):
            tmp.append(degrees_table[row][col])
        X_train2.append(tmp)
    # Get y_train
    for row in range(len(degrees_table)):
        y_train2.append(degrees_table[row][-1])
    # Create a MyDecisionTreeClassifier object
    test_fit2 = MyDecisionTreeClassifier()
    # Call fit
    test_fit2.fit(X_train2, y_train2)
    # Test
    assert degrees_tree == test_fit2.tree
Ejemplo n.º 5
0
def test_decision_tree_classifier_fit():
    # Test the fit for the Interview Dataset
    myDecisionTreeClassifier = MyDecisionTreeClassifier()
    y_train, X_train = [], []
    for inst in interview_table:
        y_train.append(inst[-1])
        X_train.append(inst[:-1])
    myDecisionTreeClassifier.fit(X_train, y_train)

    assert interview_tree == myDecisionTreeClassifier.tree

    # Test the result from Bramer
    y_train, X_train = [], []
    for inst in degrees_table:
        y_train.append(inst[-1])
        X_train.append(inst[:-1])
    myDecisionTreeClassifier.fit(X_train, y_train)

    assert degrees_tree == myDecisionTreeClassifier.tree
Ejemplo n.º 6
0
from mysklearn.myclassifiers import MyDecisionTreeClassifier
from mysklearn.mypytable import MyPyTable
import mysklearn.myutils as myutils
import mysklearn.myevaluation as myevaluation

stars_table = myutils.load_data("Stars.csv")
temperature = myutils.temp_bins(stars_table.get_column('Temperature'))
L = myutils.luminosity_bins(stars_table.get_column('L'))
R = myutils.get_radius(stars_table.get_column('R'))
a_m = myutils.get_magnitude(stars_table.get_column('A_M'))
color = myutils.categorize_colors(stars_table.get_column('Color'))
spectral_class = myutils.get_spectral_class(stars_table.get_column('Spectral_Class'))
star_type = stars_table.get_column('Type')

x_vals = [[temperature[i], str(L[i]), str(R[i]), str(a_m[i]), color[i], spectral_class[i]] for i in range(len(stars_table.data))]
y_vals = star_type

xtr, xts, ytr, yts = myevaluation.train_test_split(x_vals, y_vals)

my_tree = MyDecisionTreeClassifier()
my_tree.fit(xtr, ytr)

predicted = my_tree.predict(xts)
accuracy = myutils.compute_accuracy(predicted, yts)
print('My Decision Tree: Accuracy =', round(accuracy * 100, 3), 'Error Rate = ', round((1-accuracy) * 100, 3))

# pickle classifier
with open("decision_tree.p", "wb") as fout:
    pkl_obj = my_tree.tree
    pickle.dump(my_tree, fout)
Ejemplo n.º 7
0
def test_decision_tree_classifier_fit():
    X_train = [["Senior", "Java", "no", "no"], ["Senior", "Java", "no", "yes"],
               ["Mid", "Python", "no", "no"], ["Junior", "Python", "no", "no"],
               ["Junior", "R", "yes", "no"], ["Junior", "R", "yes", "yes"],
               ["Mid", "R", "yes", "yes"], ["Senior", "Python", "no", "no"],
               ["Senior", "R", "yes", "no"], ["Junior", "Python", "yes", "no"],
               ["Senior", "Python", "yes", "yes"],
               ["Mid", "Python", "no", "yes"], ["Mid", "Java", "yes", "no"],
               ["Junior", "Python", "no", "yes"]]

    interview_tree = \
        ['Attribute', 'att0',
            ['Value', 'Senior',
                ['Attribute', 'att2',
                    ['Value', 'no',
                        ['Leaf', 'False', 3, 5]
                    ],
                    ['Value', 'yes',
                        ['Leaf', 'True', 2, 5]
                    ]
                ]
            ],
            ['Value', 'Mid',
                ['Leaf', 'True', 4, 14]
            ],
            ['Value', 'Junior',
                ['Attribute', 'att3',
                    ['Value', 'no',
                        ['Leaf', 'True', 3, 5]
                    ],
                    ['Value', 'yes',
                        ['Leaf', 'False', 2, 5]
                    ]
                ]
            ]
        ]
    y_train = [
        "False", "False", "True", "True", "True", "False", "True", "False",
        "True", "True", "True", "True", "True", "False"
    ]
    myt = MyDecisionTreeClassifier()
    myt.fit(X_train, y_train)
    assert (myt.tree == interview_tree)

    degrees_table = [
        ["A", "B", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "A", "B", "B", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "A", "A", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "A", "B", "FIRST"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "B", "B", "SECOND"],
        ["B", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "B", "A", "A", "FIRST"],
        ["B", "B", "B", "A", "A", "SECOND"],
        ["B", "B", "A", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["B", "A", "B", "A", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "B", "A", "B", "B", "SECOND"],
        ["B", "A", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
    ]
    degrees_y_train = []
    for row in degrees_table:
        degrees_y_train.append(row[-1])
        del row[-1]

    myt.fit(degrees_table, degrees_y_train)

    degrees_tree = \
        ['Attribute', 'att0',
            ['Value', 'A',
                ['Attribute', 'att4',
                    ['Value', 'B',
                        ['Attribute', 'att3',
                            ['Value', 'B',
                                ['Leaf', 'SECOND', 7, 9]
                            ],
                            ['Value', 'A',
                                ['Attribute', 'att1',
                                    ['Value', 'B',
                                        ['Leaf', 'SECOND', 1, 2]
                                    ],
                                    ['Value', 'A',
                                        ['Leaf', 'FIRST', 1, 2]
                                    ]
                                ]
                            ]
                        ]
                    ],
                    ['Value', 'A',
                        ['Leaf', 'FIRST', 5, 14]
                    ]
                ]
            ],
            ['Value', 'B',
                ['Leaf', 'SECOND', 12, 26]
            ]
        ]

    assert (myt.tree == degrees_tree)
Ejemplo n.º 8
0
def test_decision_tree_classifier_predict():
    X_train = [["Senior", "Java", "no", "no"], ["Senior", "Java", "no", "yes"],
               ["Mid", "Python", "no", "no"], ["Junior", "Python", "no", "no"],
               ["Junior", "R", "yes", "no"], ["Junior", "R", "yes", "yes"],
               ["Mid", "R", "yes", "yes"], ["Senior", "Python", "no", "no"],
               ["Senior", "R", "yes", "no"], ["Junior", "Python", "yes", "no"],
               ["Senior", "Python", "yes", "yes"],
               ["Mid", "Python", "no", "yes"], ["Mid", "Java", "yes", "no"],
               ["Junior", "Python", "no", "yes"]]

    y_train = [
        "False", "False", "True", "True", "True", "False", "True", "False",
        "True", "True", "True", "True", "True", "False"
    ]
    tree = MyDecisionTreeClassifier()
    tree.fit(X_train, y_train)
    X_test = [["Senior", "Java", "no", "no"], ["Senior", "Java", "no", "yes"],
              ["Senior", "Java", "yes", "no"]]
    pred = tree.predict(X_test)
    assert pred == ["False", "False", "True"]  # TODO: fix this

    degrees_header = ["SoftEng", "ARIN", "HCI", "CSA", "Project", "Class"]
    degrees_table = [
        ["A", "B", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "A", "B", "B", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "A", "A", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "A", "B", "FIRST"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "B", "B", "SECOND"],
        ["B", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "B", "A", "A", "FIRST"],
        ["B", "B", "B", "A", "A", "SECOND"],
        ["B", "B", "A", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["B", "A", "B", "A", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "B", "A", "B", "B", "SECOND"],
        ["B", "A", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
    ]

    X_train = []
    y_train = []
    for row in degrees_table:
        X_train.append(row[0:4])
        y_train.append(row[4])

    tree1 = MyDecisionTreeClassifier()
    tree1.fit(X_train, y_train)

    test_vals = [["B", "B", "B", "B", "B"], ["A", "A", "A", "A", "A"],
                 ["A", "A", "A", "A", "B"]]

    assert tree1.predict(test_vals) == ['A', 'A', 'A']
Ejemplo n.º 9
0
def test_decision_tree_classifier_fit():
    X_train = [["Senior", "Java", "no", "no"], ["Senior", "Java", "no", "yes"],
               ["Mid", "Python", "no", "no"], ["Junior", "Python", "no", "no"],
               ["Junior", "R", "yes", "no"], ["Junior", "R", "yes", "yes"],
               ["Mid", "R", "yes", "yes"], ["Senior", "Python", "no", "no"],
               ["Senior", "R", "yes", "no"], ["Junior", "Python", "yes", "no"],
               ["Senior", "Python", "yes", "yes"],
               ["Mid", "Python", "no", "yes"], ["Mid", "Java", "yes", "no"],
               ["Junior", "Python", "no", "yes"]]

    y_train = [
        "False", "False", "True", "True", "True", "False", "True", "False",
        "True", "True", "True", "True", "True", "False"
    ]
    tree = MyDecisionTreeClassifier()
    tree.fit(X_train, y_train)
    interview_tree = \
    ["Attribute", "attr0",
        ["Value", "Senior",
            ["Attribute", "attr2",
                ["Value", "no",
                    ["Leaf", "False", 3, 5]
                ],
                ["Value", "yes",
                    ["Leaf", "True", 2, 5]
                ]
            ]
        ],
        ["Value", "Mid",
            ["Leaf", "True", 4, 14]
        ],
        ["Value", "Junior",
            ["Attribute", "attr3",
                ["Value", "no",
                    ["Leaf", "True", 3, 5]
                ],
                ["Value", "yes",
                    ["Leaf", "False", 2, 5]
                ]
            ]
        ]
    ]
    assert tree.tree == interview_tree
    degrees_header = ["SoftEng", "ARIN", "HCI", "CSA", "Project", "Class"]
    degrees_table = [
        ["A", "B", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "A", "B", "B", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "A", "A", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "A", "B", "FIRST"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "B", "B", "SECOND"],
        ["B", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "B", "A", "A", "FIRST"],
        ["B", "B", "B", "A", "A", "SECOND"],
        ["B", "B", "A", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["B", "A", "B", "A", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "B", "A", "B", "B", "SECOND"],
        ["B", "A", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
    ]

    degrees_result = [
        'Attribute', 'attr2',
        [
            'Value', 'A',
            [
                'Attribute', 'attr3', ['Value', 'B', ['Leaf', 'B', 7, 9]],
                [
                    'Value', 'A',
                    [
                        'Attribute', 'attr0',
                        ['Value', 'A', ['Leaf', 'A', 1, 2]],
                        ['Value', 'B', ['Leaf', 'B', 1, 2]]
                    ]
                ]
            ]
        ],
        [
            'Value', 'B',
            [
                'Attribute', 'attr1', ['Value', 'B', ['Leaf', 'A', 11, 17]],
                ['Value', 'A', ['Leaf', 'B', 6, 17]]
            ]
        ]
    ]

    X_train = []
    y_train = []
    for row in degrees_table:
        X_train.append(row[0:4])
        y_train.append(row[4])

    tree1 = MyDecisionTreeClassifier()
    tree1.fit(X_train, y_train)

    assert tree1.tree == degrees_result
Ejemplo n.º 10
0
def test_decision_tree_classifier_predict():
    ## Test #1 ##
    interview_tree = \
 ['Attribute', 'att0',
    ['Value', 'Junior',
        ['Attribute', 'att3',
            ['Value', 'no',
                ['Leaf', 'True']
            ],
            ['Value', 'yes',
                ['Leaf', 'False']
            ]
        ]
    ],
    ['Value', 'Mid',
        ['Leaf', 'True']
    ],
    ['Value', 'Senior',
        ['Attribute', 'att2',
            ['Value', 'no',
                ['Leaf', 'False']
            ],
            ['Value', 'yes',
                ['Leaf', 'True']
            ]
        ]
    ]
    ]

    X_train = [["Senior", "Java", "no", "no"], ["Senior", "Java", "no", "yes"],
               ["Mid", "Python", "no", "no"], ["Junior", "Python", "no", "no"],
               ["Junior", "R", "yes", "no"], ["Junior", "R", "yes", "yes"],
               ["Mid", "R", "yes", "yes"], ["Senior", "Python", "no", "no"],
               ["Senior", "R", "yes", "no"], ["Junior", "Python", "yes", "no"],
               ["Senior", "Python", "yes", "yes"],
               ["Mid", "Python", "no", "yes"], ["Mid", "Java", "yes", "no"],
               ["Junior", "Python", "no", "yes"]]

    y_train = [
        "False", "False", "True", "True", "True", "False", "True", "False",
        "True", "True", "True", "True", "True", "False"
    ]
    tree_classifier = MyDecisionTreeClassifier()
    tree_classifier.fit(X_train, y_train)

    prediction = tree_classifier.predict([["Junior", "Java", "yes", "no"],
                                          ["Junior", "Java", "yes", "yes"]])
    y_actual = ["True", "False"]
    assert prediction == y_actual

    ### Test #2 ###
    degree_tree =  \
    ['Attribute', 'att0',
        ['Value', 'A',
            ['Attribute', 'att4',
                ['Value', 'A',
                    ['Leaf', 'First']
                ],
                ['Value', 'B',
                    ['Attribute', 'att3',
                        ['Value', 'A',
                            ['Attribute', 'att1',
                                ['Value', 'A',
                                    ['Leaf', 'First']
                                ],
                                ['Value', 'B',
                                    ['Leaf', 'Second']
                                ]
                            ]
                        ],
                        ['Value', 'B',
                            ['Leaf', 'Second']
                        ]
                    ]
                ]
            ]
        ],
        ['Value', 'B',
            ['Leaf', 'Second']
        ]
    ]

    X_train = [["A", "B", "A", "B", "B"], ["A", "B", "B", "B", "A"],
               ["A", "A", "A", "B", "B"], ["B", "A", "A", "B", "B"],
               ["A", "A", "B", "B", "A"], ["B", "A", "A", "B", "B"],
               ["A", "B", "B", "B", "B"], ["A", "B", "B", "B", "B"],
               ["A", "A", "A", "A", "A"], ["B", "A", "A", "B", "B"],
               ["B", "A", "A", "B", "B"], ["A", "B", "B", "A", "B"],
               ["B", "B", "B", "B", "A"], ["A", "A", "B", "A", "B"],
               ["B", "B", "B", "B", "A"], ["A", "A", "B", "B", "B"],
               ["B", "B", "B", "B", "B"], ["A", "A", "B", "A", "A"],
               ["B", "B", "B", "A", "A"], ["B", "B", "A", "A", "B"],
               ["B", "B", "B", "B", "A"], ["B", "A", "B", "A", "B"],
               ["A", "B", "B", "B", "A"], ["A", "B", "A", "B", "B"],
               ["B", "A", "B", "B", "B"], ["A", "B", "B", "B", "B"]]

    y_train = [
        "Second", "First", "Second", "Second", "First", "Second", "Second",
        "Second", "First", "Second", "Second", "Second", "Second", "First",
        "Second", "Second", "Second", "First", "Second", "Second", "Second",
        "Second", "First", "Second", "Second", "Second"
    ]
    tree_classifier2 = MyDecisionTreeClassifier()
    tree_classifier2.fit(X_train, y_train)

    prediction = tree_classifier2.predict([["B", "B", "B", "B", "B"],
                                           ["A", "A", "A", "A", "A"],
                                           ["A", "A", "A", "A", "B"]])
    y_actual = ["Second", "First", "First"]
    assert prediction == y_actual
Ejemplo n.º 11
0
def test_decision_tree_classifier_predict():

    interview_header = ["level", "lang", "tweets", "phd", "interviewed_well"]
    interview_table = [["Senior", "Java", "no", "no", "False"],
                       ["Senior", "Java", "no", "yes", "False"],
                       ["Mid", "Python", "no", "no", "True"],
                       ["Junior", "Python", "no", "no", "True"],
                       ["Junior", "R", "yes", "no", "True"],
                       ["Junior", "R", "yes", "yes", "False"],
                       ["Mid", "R", "yes", "yes", "True"],
                       ["Senior", "Python", "no", "no", "False"],
                       ["Senior", "R", "yes", "no", "True"],
                       ["Junior", "Python", "yes", "no", "True"],
                       ["Senior", "Python", "yes", "yes", "True"],
                       ["Mid", "Python", "no", "yes", "True"],
                       ["Mid", "Java", "yes", "no", "True"],
                       ["Junior", "Python", "no", "yes", "False"]]
    trueTree = \
        ["Attribute", "att0",
            ["Value", "Junior",
                ["Attribute", "att3",
                    ["Value", "no",
                        ["Leaf", "True", 3, 5]
                    ],
                    ["Value", "yes",
                        ["Leaf", "False", 2, 5]
                    ]
                ]
            ],
            ["Value", "Mid",
                ["Leaf", "True", 4, 14]
            ],
            ["Value", "Senior",
                ["Attribute", "att2",
                    ["Value", "no",
                        ["Leaf", "False", 3, 5]
                    ],
                    ["Value", "yes",
                        ["Leaf", "True", 2, 5]
                    ]
                ]
            ]
        ]

    xTest = [['Junior', 'Java', 'yes', 'no'], ['Junior', 'Java', 'yes', 'yes']]
    expected = ['True', 'False']

    classes = []
    for row in interview_table:
        classes.append(row.pop(-1))

    testDT = MyDecisionTreeClassifier()
    testDT.fit(interview_table, classes)

    predictions = testDT.predict(xTest)
    assert predictions == expected

    # bramer degrees dataset
    degrees_header = ["SoftEng", "ARIN", "HCI", "CSA", "Project", "Class"]
    degrees_table = [
        ["A", "B", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "A", "B", "B", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "A", "A", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "A", "B", "FIRST"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "B", "B", "SECOND"],
        ["B", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "B", "A", "A", "FIRST"],
        ["B", "B", "B", "A", "A", "SECOND"],
        ["B", "B", "A", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["B", "A", "B", "A", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "B", "A", "B", "B", "SECOND"],
        ["B", "A", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
    ]

    degrees_tree = \
        ['Attribute', 'att0',
            ['Value', 'A',
                ['Attribute', 'att4',
                    ['Value', 'A',
                        ['Leaf', 'FIRST', 5, 14]
                    ],
                    ['Value', 'B',
                        ['Attribute', 'att3',
                            ['Value', 'A',
                                ['Attribute', 'att1',
                                    ['Value', 'A',
                                        ['Leaf', 'FIRST', 1, 2]
                                    ],
                                    ['Value', 'B',
                                        ['Leaf', 'SECOND', 1, 2]
                                    ]
                                ]
                            ],
                            ['Value', 'B',
                                ['Leaf', 'SECOND', 7, 9]
                            ]
                        ]
                    ]
                ]
            ],
            ['Value', 'B',
                ['Leaf', 'SECOND', 12, 26]
            ]
        ]

    classes = []
    for row in degrees_table:
        classes.append(row.pop(-1))

    testDT = MyDecisionTreeClassifier()
    testDT.fit(degrees_table, classes)

    xTest = [["B", "B", "B", "B", "B"], ["A", "A", "A", "A", "A"],
             ["A", "A", "A", "A", "B"]]
    expected = ['SECOND', 'FIRST', 'FIRST']

    predictions = testDT.predict(xTest)

    testDT.print_decision_rules(trueTree)

    assert predictions == expected
Ejemplo n.º 12
0
def test_decision_tree_classifier_fit():

    interview_header = ["level", "lang", "tweets", "phd", "interviewed_well"]
    interview_table = [["Senior", "Java", "no", "no", "False"],
                       ["Senior", "Java", "no", "yes", "False"],
                       ["Mid", "Python", "no", "no", "True"],
                       ["Junior", "Python", "no", "no", "True"],
                       ["Junior", "R", "yes", "no", "True"],
                       ["Junior", "R", "yes", "yes", "False"],
                       ["Mid", "R", "yes", "yes", "True"],
                       ["Senior", "Python", "no", "no", "False"],
                       ["Senior", "R", "yes", "no", "True"],
                       ["Junior", "Python", "yes", "no", "True"],
                       ["Senior", "Python", "yes", "yes", "True"],
                       ["Mid", "Python", "no", "yes", "True"],
                       ["Mid", "Java", "yes", "no", "True"],
                       ["Junior", "Python", "no", "yes", "False"]]
    trueTree = \
        ["Attribute", "att0",
            ["Value", "Junior",
                ["Attribute", "att3",
                    ["Value", "no",
                        ["Leaf", "True", 3, 5]
                    ],
                    ["Value", "yes",
                        ["Leaf", "False", 2, 5]
                    ]
                ]
            ],
            ["Value", "Mid",
                ["Leaf", "True", 4, 14]
            ],
            ["Value", "Senior",
                ["Attribute", "att2",
                    ["Value", "no",
                        ["Leaf", "False", 3, 5]
                    ],
                    ["Value", "yes",
                        ["Leaf", "True", 2, 5]
                    ]
                ]
            ]
        ]

    classes = []
    for row in interview_table:
        classes.append(row.pop(-1))

    testDT = MyDecisionTreeClassifier()
    testDT.fit(interview_table, classes)

    assert testDT.tree == trueTree  # TODO: fix this

    # bramer degrees dataset
    degrees_header = ["SoftEng", "ARIN", "HCI", "CSA", "Project", "Class"]
    degrees_table = [
        ["A", "B", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "A", "B", "B", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "A", "A", "A", "FIRST"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["B", "A", "A", "B", "B", "SECOND"],
        ["A", "B", "B", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "A", "B", "FIRST"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["A", "A", "B", "B", "B", "SECOND"],
        ["B", "B", "B", "B", "B", "SECOND"],
        ["A", "A", "B", "A", "A", "FIRST"],
        ["B", "B", "B", "A", "A", "SECOND"],
        ["B", "B", "A", "A", "B", "SECOND"],
        ["B", "B", "B", "B", "A", "SECOND"],
        ["B", "A", "B", "A", "B", "SECOND"],
        ["A", "B", "B", "B", "A", "FIRST"],
        ["A", "B", "A", "B", "B", "SECOND"],
        ["B", "A", "B", "B", "B", "SECOND"],
        ["A", "B", "B", "B", "B", "SECOND"],
    ]

    degrees_tree = \
        ['Attribute', 'att0',
            ['Value', 'A',
                ['Attribute', 'att4',
                    ['Value', 'A',
                        ['Leaf', 'FIRST', 5, 14]
                    ],
                    ['Value', 'B',
                        ['Attribute', 'att3',
                            ['Value', 'A',
                                ['Attribute', 'att1',
                                    ['Value', 'A',
                                        ['Leaf', 'FIRST', 1, 2]
                                    ],
                                    ['Value', 'B',
                                        ['Leaf', 'SECOND', 1, 2]
                                    ]
                                ]
                            ],
                            ['Value', 'B',
                                ['Leaf', 'SECOND', 7, 9]
                            ]
                        ]
                    ]
                ]
            ],
            ['Value', 'B',
                ['Leaf', 'SECOND', 12, 26]
            ]
        ]

    classes = []
    for row in degrees_table:
        classes.append(row.pop(-1))

    testDT = MyDecisionTreeClassifier()
    testDT.fit(degrees_table, classes)

    assert testDT.tree == degrees_tree