Jaccard index: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Added self pairings - every time I look at this the task description has changed!)
(→‎{{header|Factor}}: whoops, remove redundant pairings)
Line 19: Line 19:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: formatting kernel math prettyprint sequences sets ;
<lang factor>USING: assocs formatting grouping kernel math math.combinatorics
prettyprint sequences sequences.repeating sets ;


: jaccard ( seq1 seq2 -- x )
: jaccard ( seq1 seq2 -- x )
Line 26: Line 27:


{ { } { 1 2 3 4 5 } { 1 3 5 7 9 } { 2 4 6 8 10 } { 2 3 5 7 } { 8 } }
{ { } { 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
dup [ 2dup jaccard "%[%d, %] %[%d, %] -> %u\n" printf ] cartesian-each</lang>
[ 2dup jaccard "%[%d, %] %[%d, %] -> %u\n" printf ] assoc-each</lang>
{{out}}
{{out}}
<pre>
<pre>
{ } { } -> 1
{ } { 1, 2, 3, 4, 5 } -> 0
{ } { 1, 2, 3, 4, 5 } -> 0
{ } { 1, 3, 5, 7, 9 } -> 0
{ } { 1, 3, 5, 7, 9 } -> 0
Line 35: Line 36:
{ } { 2, 3, 5, 7 } -> 0
{ } { 2, 3, 5, 7 } -> 0
{ } { 8 } -> 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 } { 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, 4, 6, 8, 10 } -> 1/4
{ 1, 2, 3, 4, 5 } { 2, 3, 5, 7 } ->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