Skip to content

Commit

Permalink
removed numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Bender committed Jul 29, 2018
1 parent 065898e commit d5ded33
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions algorithms/ml/nearest_neighbor.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import numpy
import math

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"
result = ()
sum = 0
for i in range(len(x)):
result += (x[i] -y[i],)
return numpy.linalg.norm(result)
for component in result:
sum += component**2
return math.sqrt(sum)


def nearest_neighbor(x, tSet):
"""[summary]
Expand All @@ -30,7 +32,7 @@ def nearest_neighbor(x, tSet):
"""
assert isinstance(x, tuple) and isinstance(tSet, dict)
current_key = ()
min_d = numpy.inf
min_d = math.inf
for key in tSet:
d = distance(x, key)
if d < min_d:
Expand Down

0 comments on commit d5ded33

Please sign in to comment.