Last active
December 15, 2015 19:20
-
-
Save node/02d9108978c3ad4649ee to your computer and use it in GitHub Desktop.
Data
This file contains hidden or 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
#!/usr/bin/env python | |
# ref. https://github.com/saimadhu-polamuri/DataAspirant_codes/blob/master/Similarity_measures/similaritymeasures.py | |
# ref. http://hi.baidu.com/uu_master/item/0b89469b42adef9bcd80e5e8 | |
from math import* | |
from decimal import Decimal | |
class Similarity(): | |
""" Five similarity measures function """ | |
def euclidean_distance(self,x,y): | |
""" return euclidean distance between two lists """ | |
return sqrt(sum(pow(a-b,2) for a, b in zip(x, y))) | |
def manhattan_distance(self,x,y): | |
""" return manhattan distance between two lists """ | |
return sum(abs(a-b) for a,b in zip(x,y)) | |
def minkowski_distance(self,x,y,p_value): | |
""" return minkowski distance between two lists """ | |
return self.nth_root(sum(pow(abs(a-b),p_value) for a,b in zip(x, y)),p_value) | |
def nth_root(self,value, n_root): | |
""" returns the n_root of an value """ | |
root_value = 1/float(n_root) | |
return round (Decimal(value) ** Decimal(root_value),3) | |
def cosine_similarity(self,x,y): | |
""" return cosine similarity between two lists """ | |
numerator = sum(a*b for a,b in zip(x,y)) | |
denominator = self.square_rooted(x)*self.square_rooted(y) | |
return round(numerator/float(denominator),3) | |
def square_rooted(self,x): | |
""" return 3 rounded square rooted value """ | |
return round(sqrt(sum([a*a for a in x])),3) | |
def jaccard_similarity(self,x,y): | |
""" returns the jaccard similarity between two lists """ | |
intersection_cardinality = len(set.intersection(*[set(x), set(y)])) | |
union_cardinality = len(set.union(*[set(x), set(y)])) | |
return intersection_cardinality/float(union_cardinality) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment