-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decision_tree.py
More file actions
30 lines (23 loc) · 942 Bytes
/
test_decision_tree.py
File metadata and controls
30 lines (23 loc) · 942 Bytes
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
from unittest import TestCase
from decision_tree import *
class Test(TestCase):
def test_detects_purity(self):
d = [Datum(True)]
self.assertAlmostEqual(0.0, impurity(d))
def test_detects_even_impurity(self):
d = [Datum(True)] * 5 + [Datum(False)] * 5
self.assertAlmostEqual(0.5, impurity(d))
def test_detects_uneven_impurity(self):
d = [Datum(True)] * 3 + [Datum(False)]
self.assertAlmostEqual(6/16, impurity(d))
def test_computes_split_cost(self):
result = split_cost(data, 'Raining', False)
self.assertAlmostEqual(0.4857, result, delta=0.001)
def test_finds_best_split(self):
a, v = best_split(data)
self.assertEqual('Patrons', a)
self.assertEqual('Some', v)
def test_predicts_all_training_examples_correctly(self):
tree = Tree(data)
for d in data:
self.assertEqual(d.target, tree.predict(d))