Find the Jaccard Index and Jaccard Distance between the two given sets | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; set < int > intersection ( set < int > s1 , set < int > s2 ) { set < int > intersect ; set_intersection ( s1 . begin ( ) , s1 . end ( ) , s2 . begin ( ) , s2 . end ( ) , inserter ( intersect , intersect . begin ( ) ) ) ; return intersect ; } double jaccard_index ( set < int > s1 , set < int > s2 ) { double size_s1 = s1 . size ( ) ; double size_s2 = s2 . size ( ) ; set < int > intersect = intersection ( s1 , s2 ) ; double size_in = intersect . size ( ) ; double jaccard_in = size_in / ( size_s1 + size_s2 - size_in ) ; return jaccard_in ; } double jaccard_distance ( double jaccardIndex ) { double jaccard_dist = 1 - jaccardIndex ; return jaccard_dist ; } int main ( ) { set < int > s1 ; s1 . insert ( 1 ) ; s1 . insert ( 2 ) ; s1 . insert ( 3 ) ; s1 . insert ( 4 ) ; s1 . insert ( 5 ) ; set < int > s2 ; s2 . insert ( 4 ) ; s2 . insert ( 5 ) ; s2 . insert ( 6 ) ; s2 . insert ( 7 ) ; s2 . insert ( 8 ) ; s2 . insert ( 9 ) ; s2 . insert ( 10 ) ; double jaccardIndex = jaccard_index ( s1 , s2 ) ; cout << " Jaccard ▁ index ▁ = ▁ " << jaccardIndex << endl ; cout << " Jaccard ▁ distance ▁ = ▁ " << jaccard_distance ( jaccardIndex ) ; return 0 ; }