Jaccard Similarity for Sets with strings

Jaccard Similarity (J) = (number of matching strings present in both sets) / (number of strings in either of the set)

Considering A and B as two sets, it can be represented in symbolic form as

J(A, B) =  |A Ո B| / |A U B|

Example :

Let A and B be two sets of strings where

Set A = { ‘John’, ‘is’, ’going’,’ to’, ’the’, ’market’, ’today’, ’to’, ’buy’, ’cake’} and

Set B = {‘Tim’, ‘is’, ‘at’, ’the’,’ shop’, ’already’, ’for’, ‘buying’, ‘two’, ‘cakes’}

Find Jaccard Similarity between the two sets.

R




# Install package "bayesbio" and load the library
library(bayesbio)
  
# Two strings "String_A" and "String_B" as sets
String_A < - c("John", "is", "going", "to", "the",
               "market", "today", "to", "buy", "cake")
String_B < - c("Tim", "is", "at", "the", "shop",
               "already", "for", "buying", "two", "cakes")
  
# Computing Jaccard similarity between strings word 
# by word
# Note - value 0 denotes complete match and 1 denotes 
# no match as per "stringdist" function
stringdist(String_A, String_B, method='jaccard')
  
# Computing Jaccard similarity between strings overall
jaccardSets(String_A, String_B)
  
# Computing Jaccard distance
jaccard_distance = 1 - jaccardSets(String_A, String_B)
jaccard_distance


Output



How to Calculate Jaccard Similarity in R?

Jaccard Similarity also called as Jaccard Index or Jaccard Coefficient is a simple measure to represent the similarity between data samples. The similarity is computed as the ratio of the length of the intersection within data samples to the length of the union of the data samples. 

It is represented as – 

J(A, B) =  |A Ո B| / |A U B|

It is used to find the similarity or overlap between the two binary vectors or numeric vectors or strings. It can be represented as J. There is also a closely related term associated with Jaccard Similarity which is called Jaccard Dissimilarity or Jaccard Distance. Jaccard Distance is a measure of dissimilarity between data samples and can be represented as (1 – J)  where J is Jaccard Similarity.

Similar Reads

Common Applications of Jaccard Similarity:

Jaccard Similarity is used in multiple data science and machine learning applications. Some of the frequent use cases encountered in real life include :...

Jaccard Similarity for Numeric Sets:

Jaccard Similarity (J) = ( count of common elements in both sets) / ( count of elements in first set + count of elements in second set – count of common elements in both sets)...

Jaccard Similarity for Binary Sets

...

Jaccard Similarity for Sets with strings

Considering A and B as two binary vectors,...

Contact Us