Jaccard index: Difference between revisions

From Rosetta Code
Content added Content deleted
([https://en.wikipedia.org/wiki/Jaccard_index])
 
(Added Wren)
Line 4: Line 4:
The '''Jaccard index''', also known as the '''Jaccard similarity coefficient''', is a statistic used for gauging the similarity and diversity of sample sets. It was developed by Paul Jaccard, originally giving the French name ''coefficient de communauté'', and independently formulated again by T. Tanimoto. Thus, the '''Tanimoto index''' or '''Tanimoto coefficient''' are also used in some fields. However, they are identical in generally taking the ratio of '''Intersection over Union'''. The Jaccard coefficient measures similarity between finite sample sets, and is defined as the size of the intersection divided by the size of the union of the sample sets:
The '''Jaccard index''', also known as the '''Jaccard similarity coefficient''', is a statistic used for gauging the similarity and diversity of sample sets. It was developed by Paul Jaccard, originally giving the French name ''coefficient de communauté'', and independently formulated again by T. Tanimoto. Thus, the '''Tanimoto index''' or '''Tanimoto coefficient''' are also used in some fields. However, they are identical in generally taking the ratio of '''Intersection over Union'''. The Jaccard coefficient measures similarity between finite sample sets, and is defined as the size of the intersection divided by the size of the union of the sample sets:
:J(A, B) = |A ∩ B|/|A ∪ B|
:J(A, B) = |A ∩ B|/|A ∪ B|
<br><br>
=={{header|Wren}}==
{{libheader|Wren-set}}
Note that the Set object in the above module is implemented as a Map and consequently the iteration order (and the order in which elements are printed) is undefined.
<lang ecmascript>import "./set" for Set

var jacardIndex = Fn.new { |a, b|
if (a.count == 0 && b.count == 0) return 1
return a.intersect(b).count / a.union(b).count
}

var a = Set.new([1, 2, 3, 4, 5, 6])
var b = Set.new([3, 4, 5, 6, 7, 8])
System.print("a = %(a)")
System.print("b = %(b)")
System.print("J(a, b) = %(jacardIndex.call(a, b))")</lang>

{{out}}
<pre>
a = <2, 1, 3, 5, 4, 6>
b = <3, 5, 8, 7, 4, 6>
J(a, b) = 0.5
</pre>

Revision as of 23:56, 8 November 2021

Jaccard index is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
This page uses content from Wikipedia. The original article was at Jaccard index. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)

The Jaccard index, also known as the Jaccard similarity coefficient, is a statistic used for gauging the similarity and diversity of sample sets. It was developed by Paul Jaccard, originally giving the French name coefficient de communauté, and independently formulated again by T. Tanimoto. Thus, the Tanimoto index or Tanimoto coefficient are also used in some fields. However, they are identical in generally taking the ratio of Intersection over Union. The Jaccard coefficient measures similarity between finite sample sets, and is defined as the size of the intersection divided by the size of the union of the sample sets:

J(A, B) = |A ∩ B|/|A ∪ B|



Wren

Library: Wren-set

Note that the Set object in the above module is implemented as a Map and consequently the iteration order (and the order in which elements are printed) is undefined. <lang ecmascript>import "./set" for Set

var jacardIndex = Fn.new { |a, b|

   if (a.count == 0 && b.count == 0) return 1
   return a.intersect(b).count / a.union(b).count

}

var a = Set.new([1, 2, 3, 4, 5, 6]) var b = Set.new([3, 4, 5, 6, 7, 8]) System.print("a = %(a)") System.print("b = %(b)") System.print("J(a, b) = %(jacardIndex.call(a, b))")</lang>

Output:
a = <2, 1, 3, 5, 4, 6>
b = <3, 5, 8, 7, 4, 6>
J(a, b) = 0.5