In: Computer Science
Write a simple matching coefficient and jaccard similarity code in python.
For a example x = 10101 and y = 00101 what is the code to check those similarities in python?
Simple matching coefficient is useful when both positive and
negative values carried equal information. In case of binary
number, it is 0 or 1
The Jaccard similarity measures the similarity between finite
sample sets and is defined as the cardinality of the intersection
of sets divided by the cardinality of the union of the sample
sets.
The python program
from math import*
#x = 10101 and y = 00101
x = [1,0,1,0,1]
y = [0,0,1,0,1]
def simple_matching_coeff(a, b):
d1=d2=d12=0
for i in range(0,len(x)):
d1 += x[i]
d2 += y[i]
d12 += x[i]*y[i]
return d12/(d1*d2)
c = simple_matching_coeff(x,y)
print("Simple matching coefficient:", c)
def jaccard_matching_coeff(x,y):
intersection_card = len(set.intersection(*[set(x), set(y)]))
union_card = len(set.union(*[set(x), set(y)]))
return intersection_card/float(union_card)
print ("Jaccard Simalirity coefficent",
jaccard_matching_coeff(x,y))
The program output as below: