From 9e95d17eb5028cfa2efaa5da6ddb8a23e99ec13f Mon Sep 17 00:00:00 2001 From: Panagiotis Petropoulakis Date: Fri, 1 Mar 2019 18:27:18 +0200 Subject: [PATCH] manhattan distance and jaccard similarity added --- grasp.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/grasp.py b/grasp.py index d47eb98..37153fd 100644 --- a/grasp.py +++ b/grasp.py @@ -1799,6 +1799,11 @@ def distance(v1, v2): """ return sum(pow(v1.get(f, 0.0) - v2.get(f, 0.0), 2) for f in features((v1, v2))) ** 0.5 +def manhattandistance(v1, v2): + """ Returns the manhattan distance of the given vectors. + """ + return sum(abs(v1.get(f, 0.0) - v2.get(f, 0.0)) for f in features((v1, v2))) + def dot(v1, v2): """ Returns the dot product of the given vectors. """ @@ -1814,6 +1819,11 @@ def cos(v1, v2): """ return 1 - dot(v1, v2) / (norm(v1) * norm(v2) or 1) # cosine distance +def jaccard (v1, v2): + """ Returns the jaccard similarity coefficient of the given vectors (0.0-1.0). + """ + return len([f for f in features((v1,v2)) if v1.get(f, 0.0) == v2.get(f, 0.0)]) / float(len(v1)) + def knn(v, vectors=[], k=3, distance=cos): """ Returns the k nearest neighbors from the given list of vectors. """