Skip to content

Instantly share code, notes, and snippets.

@veekaybee
Last active December 30, 2021 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veekaybee/f31274222ce85f7005b29f78df3de34d to your computer and use it in GitHub Desktop.
Save veekaybee/f31274222ce85f7005b29f78df3de34d to your computer and use it in GitHub Desktop.
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)

print(jaccard(cats,dogs))

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