Find the Jaccard Index and Jaccard Distance between the two given sets | Function to return the intersection set of s1 and s2 ; Find the intersection of the two sets ; Function to return the Jaccard index of two sets ; Sizes of both the sets ; Get the intersection set ; Size of the intersection set ; Calculate the Jaccard index using the formula ; Return the Jaccard index ; Function to return the Jaccard distance ; Calculate the Jaccard distance using the formula ; Return the Jaccard distance ; Driver code ; Elements of the 1 st set ; Elements of the 2 nd set ; Print the Jaccard index and Jaccard distance
def intersection ( s1 , s2 ) : NEW_LINE INDENT intersect = s1 & s2 ; NEW_LINE return intersect ; NEW_LINE DEDENT def jaccard_index ( s1 , s2 ) : NEW_LINE INDENT size_s1 = len ( s1 ) ; NEW_LINE size_s2 = len ( s2 ) ; NEW_LINE intersect = intersection ( s1 , s2 ) ; NEW_LINE size_in = len ( intersect ) ; NEW_LINE jaccard_in = size_in / ( size_s1 + size_s2 - size_in ) ; NEW_LINE return jaccard_in ; NEW_LINE DEDENT def jaccard_distance ( jaccardIndex ) : NEW_LINE INDENT jaccard_dist = 1 - jaccardIndex ; NEW_LINE return jaccard_dist ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = set ( ) ; NEW_LINE s1 . add ( 1 ) ; NEW_LINE s1 . add ( 2 ) ; NEW_LINE s1 . add ( 3 ) ; NEW_LINE s1 . add ( 4 ) ; NEW_LINE s1 . add ( 5 ) ; NEW_LINE s2 = set ( ) ; NEW_LINE s2 . add ( 4 ) ; NEW_LINE s2 . add ( 5 ) ; NEW_LINE s2 . add ( 6 ) ; NEW_LINE s2 . add ( 7 ) ; NEW_LINE s2 . add ( 8 ) ; NEW_LINE s2 . add ( 9 ) ; NEW_LINE s2 . add ( 10 ) ; NEW_LINE jaccardIndex = jaccard_index ( s1 , s2 ) ; NEW_LINE print ( " Jaccard ▁ index ▁ = ▁ " , jaccardIndex ) ; NEW_LINE print ( " Jaccard ▁ distance ▁ = ▁ " , jaccard_distance ( jaccardIndex ) ) ; NEW_LINE DEDENT