python code to compute jaccard index
Computing Jaccard Index (Jaccard similarity coefficient) is easy. Here is my first python implementation of jaccard index:
But we can make it more efficient. If you think for a moment, you will find that we really don't need to compute the union set, rather the cardinality. So this code works better:
def compute_jaccard_index(set_1, set_2):
return len(set_1.intersection(set_2)) / float(len(set_1.union(set_2)))
But we can make it more efficient. If you think for a moment, you will find that we really don't need to compute the union set, rather the cardinality. So this code works better:
def compute_jaccard_index(set_1, set_2):
n = len(set_1.intersection(set_2))
return n / float(len(set_1) + len(set_2) - n)
Comments
As you are a Python enthusiast you can join this group: https://www.facebook.com/groups/pythonbd/.
Thanks.