forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ml.py
31 lines (25 loc) · 1.2 KB
/
test_ml.py
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
from algorithms.ml.nearest_neighbor import (
distance,
nearest_neighbor
)
import unittest
class TestML(unittest.TestCase):
def setUp(self):
# train set for the AND-function
self.trainSetAND = {(0,0) : 0, (0,1) :0, (1,0) : 0, (1,1) : 1}
# train set for light or dark colors
self.trainSetLight = {(11, 98, 237) : 'L', (3, 39, 96) : 'D', (242, 226, 12) : 'L', (99, 93, 4) : 'D',
(232, 62, 32) : 'L', (119, 28, 11) : 'D', (25, 214, 47) : 'L', (89, 136, 247) : 'L',
(21, 34, 63) : 'D', (237, 99, 120) : 'L', (73, 33, 39) : 'D'}
def test_nearest_neighbor(self):
# AND-function
self.assertEqual(nearest_neighbor((1,1), self.trainSetAND), 1)
self.assertEqual(nearest_neighbor((0,1), self.trainSetAND), 0)
# dark/light color test
self.assertEqual(nearest_neighbor((31, 242, 164), self.trainSetLight), 'L')
self.assertEqual(nearest_neighbor((13, 94, 64), self.trainSetLight), 'D')
self.assertEqual(nearest_neighbor((230, 52, 239), self.trainSetLight), 'L')
def test_distance(self):
self.assertAlmostEqual(distance((1,2,3), (1,0,-1)), 4.47, 2)
if __name__ == "__main__":
unittest.main()