1+ from algorithms .ml .nearest_neighbor import (
2+ distance ,
3+ nearest_neighbor
4+ )
5+
6+ import unittest
7+
8+ class TestML (unittest .TestCase ):
9+ def setUp (self ):
10+ # train set for the AND-function
11+ self .trainSetAND = {(0 ,0 ) : 0 , (0 ,1 ) :0 , (1 ,0 ) : 0 , (1 ,1 ) : 1 }
12+
13+ # train set for light or dark colors
14+ self .trainSetLight = {(11 , 98 , 237 ) : 'L' , (3 , 39 , 96 ) : 'D' , (242 , 226 , 12 ) : 'L' , (99 , 93 , 4 ) : 'D' ,
15+ (232 , 62 , 32 ) : 'L' , (119 , 28 , 11 ) : 'D' , (25 , 214 , 47 ) : 'L' , (89 , 136 , 247 ) : 'L' ,
16+ (21 , 34 , 63 ) : 'D' , (237 , 99 , 120 ) : 'L' , (73 , 33 , 39 ) : 'D' }
17+ def test_nearest_neighbor (self ):
18+ # AND-function
19+ self .assertEqual (nearest_neighbor ((1 ,1 ), self .trainSetAND ), 1 )
20+ self .assertEqual (nearest_neighbor ((0 ,1 ), self .trainSetAND ), 0 )
21+
22+ # dark/light color test
23+ self .assertEqual (nearest_neighbor ((31 , 242 , 164 ), self .trainSetLight ), 'L' )
24+ self .assertEqual (nearest_neighbor ((13 , 94 , 64 ), self .trainSetLight ), 'D' )
25+ self .assertEqual (nearest_neighbor ((230 , 52 , 239 ), self .trainSetLight ), 'L' )
26+ def test_distance (self ):
27+ self .assertAlmostEqual (distance ((1 ,2 ,3 ), (1 ,0 ,- 1 )), 4.47 , 2 )
28+
29+
30+ if __name__ == "__main__" :
31+ unittest .main ()
0 commit comments