Essential Math for Machine Learning: Jaccard Similarity

Comparing Two Datasets

Dagang Wei
2 min readMar 20, 2024
Source

This article is part of the series Essential Math for Machine Learning.

Introduction

Have you ever wondered how to determine whether two groups of items share common elements? In the world of data analysis, the Jaccard Similarity (or Jaccard Index) is your answer. Let’s delve into what it is, why it’s useful, and see some practical examples!

What is Jaccard Similarity?

In a nutshell, Jaccard Similarity measures how much two sets have in common. Picture two shopping lists. Jaccard Similarity would help you discover how many items overlap on both lists.

The technical definition:

  • It’s the size of the intersection of the two sets divided by the size of their union.

Mathematical Representation

Jaccard Similarity (A, B) =  |A ∩ B| / |A ∪ B|

Where:

  • |A ∩ B| is the number of elements present in both sets.
  • |A ∪ B| is the total number of elements across the two sets combined.

The Range of Jaccard Similarity

Jaccard Similarity values fall between 0 and 1:

  • 0: The sets are completely different, sharing no elements.
  • 1: The sets are identical.

Putting Jaccard Similarity into Practice

Let’s see Jaccard Similarity in a few scenarios:

  • Text Comparison Want to see how similar two customer reviews are? Tokenize the reviews (break them into words) and use Jaccard Similarity to check the proportion of overlapping words.
  • Recommender Systems A streaming platform can use Jaccard Similarity to recommend movies to viewers. By examining the overlap in movies watched by different users, they can suggest ones a viewer might enjoy.
  • Plagiarism Detection It can assist in determining the level of similarity between two pieces of text, thus aiding in the detection of potential plagiarism.

Example: Jaccard Similarity in Action

Let’s imagine these sets:

  • Set A: {Apple, Banana, Orange, Grape}
  • Set B: {Banana, Pineapple, Kiwi, Apple}
  • Intersection (A ∩ B): {Apple, Banana}
  • Union (A ∪ B): {Apple, Banana, Kiwi, Orange, Grape, Pineapple}

Jaccard Similarity = 2 / 6 = 0.33

This indicates a moderate degree of similarity between the two sets.

Conclusion

The Jaccard Similarity is a versatile tool for comparing sets across various domains. If you find yourself needing to analyze set similarities, definitely keep it in your data analysis toolbox!

--

--