Skip to content

Instantly share code, notes, and snippets.

Last active December 30, 2021 15:41
What would you like to do?
Different Distance Measures

Jaccard Similarity

import numpy as numpy
import typing
a = [1,2,3,4,5,11,12]
b = [2,3,4,5,6,8,9]

cats = ["calico", "tabby", "tom"]
dogs = ["collie", "tom","bassett"]

def jaccard(list1: list, list2: list)-> float:
	intersection = len(list(set(list1).intersection(list2)))
	union  = (len(set((list1)) + set(len(list2))) - intersection
	return float(intersection/union)


jaccardSimilarity in Scala

val aVals: Seq[Int] = Seq(1,2,3,4,5,11,12)
val bVals: Seq[Int]  = Seq(2,3,4,5,6,8,9)

def calculateJaccard[T](a: Seq[T], b: Seq[T]): Double = a.intersect(b).size / a.union(b).size.toDouble

println(calculateJaccard(aVals, bVals))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment