Sitemap

Vectors similarity. Jaccard Similarity.

4 min readDec 18, 2023

--

the image generated by Image Creator from MS

In one of my previous posts, I described the essence of vector similarity search and there I explained the two most popular vector similarities — Euclidean distance and Cosine similarity. Now it’s time to cover Jaccard Similarity which I mentioned but didn’t dive into.

Idea behind.

Jaccard Similarity also has the name Jaccard Index or Jaccard similarity coefficient.

Initially, it was developed (as far as history knows) by American geologist Grove Karl Gilbert in 1884. Later it was also independently discovered by formulated by T. Tanimoto. However, the coefficient was named after Swiss botanist and geologist Paul Jaccard who came up with a coefficient as a way to compare different compositions of species in different locations.

The idea behind is quite simple it uses the concepts of the intersection of two sets and their union and calculates the similarity using the next formula:

Let’s take a look at the next example:

locationA = { Wolf, Turtle, Baobab }

locationB = { Baobab, Wolf, Leopard, Sunflower }

So the intersection will look like { Wolf, Baobab } and the union will be { Wolf, Turtle, Baobab, Leopard, Sunflower }.

Now, let’s calculate the Jaccard Similarity:

So, the Jaccard Similarity between Location A and Location B is 0.4, or 40%. This indicates the degree of similarity based on the shared species in the two locations.

Get Alexander Shmyga’s stories in your inbox

Join Medium for free to get updates from this writer.

Now let’s look into more visual representation:

Press enter or click to view image in full size
from wiki

A ⋂ B represents intersection.

Press enter or click to view image in full size
from wiki

A ⋃ B represents union.

So the math formula is looking like that:

The result of such calculation always varies between 0 and 1, when 1 is a total match and 0 is no similarities at all.

Simple implementation in C#

static double CalculateJaccardSimilarity<T>(IEnumerable<T> vector1, IEnumerable<T> vector2) {
// Convert lists to sets for easier intersection and union operations
var setA = new HashSet<T>(vector1);
var setB = new HashSet<T>(vector2);

// Calculate intersection size
int intersectionSize = 0;
foreach (T item in setA) {
if (setB.Contains(item)) {
intersectionSize++;
}
}

// Calculate union size
int unionSize = setA.Count + setB.Count - intersectionSize;

// Calculate Jaccard Similarity
double similarity = (double)intersectionSize / unionSize;

return similarity;
}

When to use Jaccard Similarity

Considering the nature of the similarity calculation it can be quite useful when the focus is on the presence or absence of elements in sets that can play a crucial role in similarities between two vectors.

Recommendation Systems. Jaccard Similarity can be employed to find similarities in users’ item preferences. By comparing the sets of items liked by different users, recommendation systems can suggest items that are likely to be of interest to a particular user

Biology and Genetics. The comparison method is used to compare the presence or absence of specific genetic markers or species in different samples. This is valuable for understanding biodiversity and genetic relationships.

Social Network Analysis. Can measure the similarity of friends or connections between individuals in a social network. It helps identify common interests or affiliations among network members.

Image Analysis. Can be applied to compare sets of features extracted from images. This is useful for tasks such as image retrieval, where the goal is to find similar images based on visual content.

Text Analysis. For example, in natural language processing, Jaccard Similarity helps identify common words or phrases between documents. It’s useful for information retrieval systems to recommend similar documents based on shared content.

Anomaly Detection. Can be used to detect anomalies by comparing the set of observed events with a reference set. Deviations from the expected pattern can indicate potential anomalies or outliers.

Data Clustering. Jaccard Similarity is employed in clustering algorithms to group similar data points. It helps identify patterns or clusters based on shared characteristics within a dataset

--

--

Alexander Shmyga
Alexander Shmyga

Written by Alexander Shmyga

🚀 Software Architecture | Software Engineering | Leadership | 🎵 Music Aficionado | ♟️ Chess Enthusiast | 🌐 World explorer