-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_DecisionTree.py
More file actions
92 lines (69 loc) · 2.81 KB
/
Copy pathtest_DecisionTree.py
File metadata and controls
92 lines (69 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import unittest
from classifierAgents import DecisionTree
import numpy as np
class test_DecisionTree(unittest.TestCase):
def test_get_purity(self):
self.tree = DecisionTree()
input =[6, 4]
test = self.tree.purity(input)
answer = 0.97
self.assertEqual(answer,round(test,2))
def test_information_gain(self):
self.tree = DecisionTree()
Y = [True,True,False,True,True,True,False,False,True,False]
X = ["Action","Comedy","Drama","Comedy","Action","Drama","Comedy","Action","Drama","Action"]
answer = 0.951
test = self.tree.weightedEntropies(X,Y)
self.assertEqual(answer,np.round(test,3))
def test_split(self):
testX = {}
testY = {}
new = np.array([False, False, True, True, False, False, False, True, True, True])
type = np.array(["Action", "Comedy", "Drama", "Comedy", "Action", "Drama", "Comedy", "Action", "Drama", "Action"])
lang = np.array(["Eng", "Sp", "Eng", "Sp", "Sp", "Sp", "Fr", "Sp", "Eng", "Fr"])
X = np.array([new, type, lang]).transpose()
# class labels
Y = np.array([True, True, False, True, True, True, False, False, True, False])
# MANUALLY SPLIT SETS - split by Language
# eng split
newEng = [False, True, True]
typeEng = ["Action", "Drama", "Drama"]
xEng = np.array([newEng, typeEng]).transpose()
yEng = np.array([True, False, True])
testX["Eng"] = xEng
testY["Eng"] = yEng
# sp split
newSp = [False, True, False, False, True]
typeSp = ["Comedy", "Comedy", "Action", "Drama", "Action"]
xSp = np.array([newSp, typeSp]).transpose()
ySp = [True, True, True, True, False]
testX["Sp"] = xSp
testY["Sp"] = ySp
newFr = [False, True]
typeFr = ["Comedy", "Action"]
xFr = np.array([newFr, typeFr]).transpose()
yFr = [False, False]
testX["Fr"] = xFr
testY["Fr"] = yFr
test = {}
test["X"] = testX
test["Y"] = testY
# class labels
self.tree = DecisionTree()
result = self.tree.split(X, Y)
self.assertItemsEqual(result, test)
def suite():
suite = unittest.TestSuite()
suite.addTest(test_DecisionTree("test_split"))
return suite
if __name__ == '__main__':
result = suite()
result.debug()
# {'y': {
# 'Fr': [False, False],
# 'Sp': [True, True, True, True, False],
# 'Eng': array([ True, False, True])},
# 'x': {
# 'Fr': array([['False', 'Comedy'], ['True', 'Action']], dtype='|S6'),
# 'Sp': array([['False', 'Comedy'], ['True', 'Comedy'], ['False', 'Action'], ['False', 'Drama'], ['True', 'Action']], dtype='|S6'),
# 'Eng': array([['False','Action'], ['True', 'Drama'],['True