Skip to content

Commit 010dce3

Browse files
author
Christian Bender
authored
Merge pull request keon#379 from christianbender/add_machine_learning
added nearest neighbor algorithm - machine-learning
2 parents b7d5db1 + 0840418 commit 010dce3

3 files changed

Lines changed: 74 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ If you want to uninstall algorithms, it is as simple as:
350350
- [simplify_path](algorithms/unix/path/simplify_path.py)
351351
- [union-find](algorithms/union-find)
352352
- [count_islands](algorithms/union-find/count_islands.py)
353+
- [machine-learning](algorithms/machine-learning)
354+
- [nearest neighbor classification](algorithms/machine-learning/nearest_neighbor.py)
353355

354356
## Contributors
355357
The repo is maintained by

algorithms/ml/nearest_neighbor.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import math
2+
3+
def distance(x,y):
4+
"""[summary]
5+
HELPER-FUNCTION
6+
calculates the (eulidean) distance between vector x and y.
7+
8+
Arguments:
9+
x {[tuple]} -- [vector]
10+
y {[tuple]} -- [vector]
11+
"""
12+
assert len(x) == len(y), "The vector must have same length"
13+
result = ()
14+
sum = 0
15+
for i in range(len(x)):
16+
result += (x[i] -y[i],)
17+
for component in result:
18+
sum += component**2
19+
return math.sqrt(sum)
20+
21+
22+
def nearest_neighbor(x, tSet):
23+
"""[summary]
24+
Implements the nearest neighbor algorithm
25+
26+
Arguments:
27+
x {[tupel]} -- [vector]
28+
tSet {[dict]} -- [training set]
29+
30+
Returns:
31+
[type] -- [result of the AND-function]
32+
"""
33+
assert isinstance(x, tuple) and isinstance(tSet, dict)
34+
current_key = ()
35+
min_d = float('inf')
36+
for key in tSet:
37+
d = distance(x, key)
38+
if d < min_d:
39+
min_d = d
40+
current_key = key
41+
return tSet[current_key]

tests/test_ml.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

Comments
 (0)