Jaccard Similarity for Binary Sets

Considering A and B as two binary vectors,

Jaccard Similarity (J) = (number of observations which are 1 in both the vectors) / (number of observations which are 1 in both the vectors + number of observations which are 0 for A and 1 for B + number of observations which are 1 for A and 0 for B)

The symbolic form becomes 

 J(A, B) =  a_11 / (a_11 + b_01 + c_10)

Where a_11 = observations being 1 in both vectors

  • b_01 = observations being 0 in A and 1 in B vector
  • c_10 = observations being 1 in A and 0 in B vector
  • d_00 = observations being 0 in both vectors ( is not required for computation of Jaccard Similarity)

Example:

Consider a grocery store selling multiple products wherein the shop owner is interested in finding out the similarity between two customers on the basis of purchases made.  Here 1 indicates the product that has been purchased by the two customers and 0 indicates that the product was not purchased by those two customers.

 

Product1

Product2

Product3

Product4

Product5

Product6

Product7

Product8

Product9

Product10

Customer1

0

1

0

0

0

1

0

0

1

1

Customer2

0

0

1

0

0

0

0

0

1

1

R




# Install packages qvalue and jaccard and load
# the library
library(qvalue)
library(jaccard)
  
# Binary vectors A and B depicting purchase of 
# items by customers
Binary_A <- c(0,1,0,0,0,1,0,0,1,1)
Binary_B <- c(0,0,1,0,0,0,0,0,1,1)
  
# Computing jaccard similarity between 2 binary 
# vectors A and B
jaccard(Binary_A,Binary_B)
  
# Computing jaccard distance between 2 binary 
# vectors A and B
Jaccard_distance <- 1 - jaccard(Binary_A,Binary_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