Jaccard index: Difference between revisions

Content added Content deleted
No edit summary
(Add Factor)
Line 16: Line 16:
Write a program that computes the Jaccard index for every pairing of these sets, including self-pairings.
Write a program that computes the Jaccard index for every pairing of these sets, including self-pairings.
<br><br>
<br><br>

=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting kernel math prettyprint sequences 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 } }
dup [ 2dup jaccard "%[%d, %] %[%d, %] -> %u\n" printf ] cartesian-each</lang>
{{out}}
<pre>
{ } { } -> 1
{ } { 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 } { } -> 0
{ 1, 2, 3, 4, 5 } { 1, 2, 3, 4, 5 } -> 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, 3, 5, 7, 9 } { } -> 0
{ 1, 3, 5, 7, 9 } { 1, 2, 3, 4, 5 } -> 3/7
{ 1, 3, 5, 7, 9 } { 1, 3, 5, 7, 9 } -> 1
{ 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 } { } -> 0
{ 2, 4, 6, 8, 10 } { 1, 2, 3, 4, 5 } -> 1/4
{ 2, 4, 6, 8, 10 } { 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