Jaccard index: Difference between revisions

From Rosetta Code
Content added Content deleted
(julia example)
(→‎{{header|Wren}}: Updated yet again!)
Line 133: Line 133:
var e = Set.new([2, 3, 5, 7])
var e = Set.new([2, 3, 5, 7])
var f = Set.new([8])
var f = Set.new([8])
var sets = [a, b, c, d, e, f]
var isets = Indexed.new([a, b, c, d, e, f])
for (se in isets) {

var i = String.fromByte(se.index + 65)
for (se in Indexed.new(sets)) {
var i = se.index
var v = se.value
v = v.toList.sort() // force original sorted order
var s = se.value
Fmt.print("$s = $n", i, v)
s = s.toList.sort() // force original sorted order
Fmt.print("$s = $n", String.fromByte(65 + i), s)
}
}

var pairs = [
[a, a], [a, b], [a, c], [a, d], [a, e], [a, f], [b, b], [b, c], [b, d], [b, e], [b, f],
[c, c], [c, d], [c, e], [c, f], [d, d], [d, e], [d, f], [e, e], [e, f], [f, f]
]

var names = [
"AA", "AB", "AC", "AD", "AE", "AF", "BB", "BC", "BD", "BE", "BF",
"CC", "CD", "CE", "CF", "DD", "DE", "DF", "EE", "EF", "FF"
]

System.print()
System.print()
for (se in Indexed.new(pairs)) {
for (se1 in isets) {
var n = names[se.index]
var i1 = String.fromByte(se1.index + 65)
var ss = se.value
var v1 = se1.value
for (se2 in isets) {
Fmt.print("J($s, $s) = $h", n[0], n[1], jacardIndex.call(ss[0], ss[1]))
var i2 = String.fromByte(se2.index + 65)
var v2 = se2.value
Fmt.print("J($s, $s) = $h", i1, i2, jacardIndex.call(v1, v2))
}
}</lang>
}</lang>


Line 174: Line 166:
J(A, E) = 0
J(A, E) = 0
J(A, F) = 0
J(A, F) = 0
J(B, A) = 0
J(B, B) = 1
J(B, B) = 1
J(B, C) = 0.428571
J(B, C) = 0.428571
Line 179: Line 172:
J(B, E) = 0.5
J(B, E) = 0.5
J(B, F) = 0
J(B, F) = 0
J(C, A) = 0
J(C, B) = 0.428571
J(C, C) = 1
J(C, C) = 1
J(C, D) = 0
J(C, D) = 0
J(C, E) = 0.5
J(C, E) = 0.5
J(C, F) = 0
J(C, F) = 0
J(D, A) = 0
J(D, B) = 0.25
J(D, C) = 0
J(D, D) = 1
J(D, D) = 1
J(D, E) = 0.125
J(D, E) = 0.125
J(D, F) = 0.2
J(D, F) = 0.2
J(E, A) = 0
J(E, B) = 0.5
J(E, C) = 0.5
J(E, D) = 0.125
J(E, E) = 1
J(E, E) = 1
J(E, F) = 0
J(E, F) = 0
J(F, A) = 0
J(F, B) = 0
J(F, C) = 0
J(F, D) = 0.2
J(F, E) = 0
J(F, F) = 1
J(F, F) = 1
</pre>
</pre>

Revision as of 08:45, 9 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|

Define sets as follows, using any linear data structure:

A = {}
B = {1, 2, 3, 4, 5}
C = {1, 3, 5, 7, 9}
D = {2, 4, 6, 8, 10}
E = {2, 3, 5, 7}
F = {8}

Write a program that computes the Jaccard index for every ordered pairing (to show that J(A, B) and J(B, A) are the same) of these sets, including self-pairings.

Factor

Works with: Factor version 0.99 2021-06-02

<lang factor>USING: assocs formatting grouping kernel math math.combinatorics prettyprint sequences sequences.repeating sets ;

jaccard ( seq1 seq2 -- x )
   2dup [ empty? ] both? [ 2drop 1 ]
   [ [ intersect ] [ union ] 2bi [ length ] bi@ / ] if ;

{ { } { 1 2 3 4 5 } { 1 3 5 7 9 } { 2 4 6 8 10 } { 2 3 5 7 } { 8 } } [ 2 <combinations> ] [ 2 repeat 2 group append ] bi [ 2dup jaccard "%u %u -> %u\n" printf ] assoc-each</lang>

Output:
{ } { 1 2 3 4 5 } -> 0
{ } { 1 3 5 7 9 } -> 0
{ } { 2 4 6 8 10 } -> 0
{ } { 2 3 5 7 } -> 0
{ } { 8 } -> 0
{ 1 2 3 4 5 } { 1 3 5 7 9 } -> 3/7
{ 1 2 3 4 5 } { 2 4 6 8 10 } -> 1/4
{ 1 2 3 4 5 } { 2 3 5 7 } -> 1/2
{ 1 2 3 4 5 } { 8 } -> 0
{ 1 3 5 7 9 } { 2 4 6 8 10 } -> 0
{ 1 3 5 7 9 } { 2 3 5 7 } -> 1/2
{ 1 3 5 7 9 } { 8 } -> 0
{ 2 4 6 8 10 } { 2 3 5 7 } -> 1/8
{ 2 4 6 8 10 } { 8 } -> 1/5
{ 2 3 5 7 } { 8 } -> 0
{ } { } -> 1
{ 1 2 3 4 5 } { 1 2 3 4 5 } -> 1
{ 1 3 5 7 9 } { 1 3 5 7 9 } -> 1
{ 2 4 6 8 10 } { 2 4 6 8 10 } -> 1
{ 2 3 5 7 } { 2 3 5 7 } -> 1
{ 8 } { 8 } -> 1

Julia

<lang julia>J(A, B) = begin i, u = length(A ∩ B), length(A ∪ B); u == 0 ? NaN : i // u end

A = Int[] B = [1, 2, 3, 4, 5] C = [1, 3, 5, 7, 9] D = [2, 4, 6, 8, 10] E = [2, 3, 5, 7] F = [8] testsets = [A, B, C, D, E, F]

println("Set A Set B J(A, B)\n", "-"^44) for a in testsets, b in testsets

   println(rpad(isempty(a) ? "[]" : a, 18), rpad(isempty(b) ? "[]" : b, 18),
       replace(string(J(a, b)), "//" => "/"))

end

</lang>

Output:
Set A             Set B             J(A, B)
--------------------------------------------
[]                []                NaN
[]                [1, 2, 3, 4, 5]   0/1
[]                [1, 3, 5, 7, 9]   0/1
[]                [2, 4, 6, 8, 10]  0/1
[]                [2, 3, 5, 7]      0/1
[]                [8]               0/1
[1, 2, 3, 4, 5]   []                0/1
[1, 2, 3, 4, 5]   [1, 2, 3, 4, 5]   1/1
[1, 2, 3, 4, 5]   [1, 3, 5, 7, 9]   3/7
[1, 2, 3, 4, 5]   [2, 4, 6, 8, 10]  1/4
[1, 2, 3, 4, 5]   [2, 3, 5, 7]      1/2
[1, 2, 3, 4, 5]   [8]               0/1
[1, 3, 5, 7, 9]   []                0/1
[1, 3, 5, 7, 9]   [1, 2, 3, 4, 5]   3/7
[1, 3, 5, 7, 9]   [1, 3, 5, 7, 9]   1/1
[1, 3, 5, 7, 9]   [2, 4, 6, 8, 10]  0/1
[1, 3, 5, 7, 9]   [2, 3, 5, 7]      1/2
[1, 3, 5, 7, 9]   [8]               0/1
[2, 4, 6, 8, 10]  []                0/1
[2, 4, 6, 8, 10]  [1, 2, 3, 4, 5]   1/4
[2, 4, 6, 8, 10]  [1, 3, 5, 7, 9]   0/1
[2, 4, 6, 8, 10]  [2, 4, 6, 8, 10]  1/1
[2, 4, 6, 8, 10]  [2, 3, 5, 7]      1/8
[2, 4, 6, 8, 10]  [8]               1/5
[2, 3, 5, 7]      []                0/1
[2, 3, 5, 7]      [1, 2, 3, 4, 5]   1/2
[2, 3, 5, 7]      [1, 3, 5, 7, 9]   1/2
[2, 3, 5, 7]      [2, 4, 6, 8, 10]  1/8
[2, 3, 5, 7]      [2, 3, 5, 7]      1/1
[2, 3, 5, 7]      [8]               0/1
[8]               []                0/1
[8]               [1, 2, 3, 4, 5]   0/1
[8]               [1, 3, 5, 7, 9]   0/1
[8]               [2, 4, 6, 8, 10]  1/5
[8]               [2, 3, 5, 7]      0/1
[8]               [8]               1/1


Wren

Library: Wren-set
Library: Wren-trait
Library: Wren-fmt

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 import "./trait" for Indexed import "./fmt" for Fmt

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([]) var b = Set.new([1, 2, 3, 4, 5]) var c = Set.new([1, 3, 5, 7, 9]) var d = Set.new([2, 4, 6, 8, 10]) var e = Set.new([2, 3, 5, 7]) var f = Set.new([8]) var isets = Indexed.new([a, b, c, d, e, f]) for (se in isets) {

   var i = String.fromByte(se.index + 65)
   var v = se.value
   v = v.toList.sort() // force original sorted order
   Fmt.print("$s = $n", i, v)

} System.print() for (se1 in isets) {

   var i1 = String.fromByte(se1.index + 65)
   var v1 = se1.value
   for (se2 in isets) {
       var i2 = String.fromByte(se2.index + 65)
       var v2 = se2.value
       Fmt.print("J($s, $s) = $h", i1, i2, jacardIndex.call(v1, v2))
   }

}</lang>

Output:
A = []
B = [1, 2, 3, 4, 5]
C = [1, 3, 5, 7, 9]
D = [2, 4, 6, 8, 10]
E = [2, 3, 5, 7]
F = [8]

J(A, A) = 1       
J(A, B) = 0       
J(A, C) = 0       
J(A, D) = 0       
J(A, E) = 0       
J(A, F) = 0       
J(B, A) = 0       
J(B, B) = 1       
J(B, C) = 0.428571
J(B, D) = 0.25    
J(B, E) = 0.5     
J(B, F) = 0       
J(C, A) = 0       
J(C, B) = 0.428571
J(C, C) = 1       
J(C, D) = 0       
J(C, E) = 0.5     
J(C, F) = 0       
J(D, A) = 0       
J(D, B) = 0.25    
J(D, C) = 0       
J(D, D) = 1       
J(D, E) = 0.125   
J(D, F) = 0.2     
J(E, A) = 0       
J(E, B) = 0.5     
J(E, C) = 0.5     
J(E, D) = 0.125   
J(E, E) = 1       
J(E, F) = 0       
J(F, A) = 0       
J(F, B) = 0       
J(F, C) = 0       
J(F, D) = 0.2     
J(F, E) = 0       
J(F, F) = 1