forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christian Bender
committed
Jul 24, 2018
1 parent
7391883
commit dd52a82
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import numpy | ||
|
||
# train set for the AND-function | ||
trainSetAND = {(0,0) : 0, (0,1) :0, (1,0) : 0, (1,1) : 1} | ||
|
||
# train set for light or dark colors | ||
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 distance(x,y): | ||
"""[summary] | ||
HELPER-FUNCTION | ||
calculates the (eulidean) distance between vector x and y. | ||
We use the numpy libriary | ||
Arguments: | ||
x {[tuple]} -- [vector] | ||
y {[tuple]} -- [vector] | ||
""" | ||
assert len(x) == len(y), "The vector must have same length" | ||
import math | ||
result = () | ||
for i in range(len(x)): | ||
result += (x[i] -y[i],) | ||
return numpy.linalg.norm(result) | ||
|
||
def nearest_neighbor(x, tSet): | ||
"""[summary] | ||
Implements the nearest neighbor algorithm | ||
Arguments: | ||
x {[tupel]} -- [vector] | ||
tSet {[dict]} -- [training set] | ||
Returns: | ||
[type] -- [result of the AND-function] | ||
""" | ||
assert isinstance(x, tuple) and isinstance(tSet, dict) | ||
currentKey = () | ||
MAX = 32768 # max value | ||
minD = MAX | ||
for key in tSet: | ||
d = distance(x, key) | ||
if d < minD: | ||
minD = d | ||
currentKey = key | ||
return tSet[currentKey] | ||
|
||
# Some test cases | ||
|
||
# print(nearest_neighbor((1,1), trainSetAND)) # => 1 | ||
# print(nearest_neighbor((0,1), trainSetAND)) # => 0 | ||
# print(nearest_neighbor((31, 242, 164), trainSetLight)) # => L | ||
# print(nearest_neighbor((13, 94, 64), trainSetLight)) # => D | ||
# print(nearest_neighbor((230, 52, 239), trainSetLight)) # => L |