JPhrases/RankClass

From J Wiki
Jump to: navigation, search

6B. Ranking & Classification

Ranking is the assignment of an ordinal number to each item of an array, and equal values are given the same rank, with the next rank assigned displaced by the number of ties. Ranking may be exemplified by considering a list of scores in some game. In golf or the card game of hearts a low score is better than a high score. In bowling or gin rummy a high score is better. In a contest where two or more players have equal scores they are ranked equally, but players with the next score are separated in rank by the number of players having equal rank.

m0=: i.~/:~ Rank y rising, 0-origin, ties equal
m1=: >:@m0 Rank y rising, 1-origin, ties equal
m2=: i.~\:~ Rank y falling, 0-origin, ties equal
m3=: >:@m2 Rank y falling, 1-origin, ties equal
m4=: /:@/: Rank y rising, 0-origin, distinct ranks
m5=: /:@\: Rank y falling, 0-origin, distinct ranks
m6=: - <./ Normalize y so that minimum atom is 0
d7=: ] * [ % [: >./ ] Scale y by x%max y
d8=: <:@[ <.[ <.@d7 m6@] Classify y into x equal intervals
d9=: [:+/"1 ([:i. [:{. ]) =/ ([:<. ([ - 1: { ]) % ([:{: ])) Classify x into {.y classes, minimum 1{y, width {:y
d10=: contour=: d12 d11 ] Classify altitudes x into contour levels y
d11=: ([: <: [: +/ ] <:/ [) { ]
d12=: [ >. <./@]
   ]z=: (],m0,m1,m2,m3,m4,m5,:m6) y=: 4 2 1 1 2 0 2 5 5 0
4 2 1 1 2 0 2 5 5 0     y
7 4 2 2 4 0 4 8 8 0     m0
8 5 3 3 5 1 5 9 9 1     m1
2 3 6 6 3 8 3 0 0 8     m2
3 4 7 7 4 9 4 1 1 9     m3
7 4 2 3 5 0 6 8 9 1     m4
2 3 6 7 4 8 5 0 1 9     m5
4 2 1 1 2 0 2 5 5 0     m6

   0.2 ": 10 d7"1 z        NB. Scale
8.00 4.00 2.00 2.00 4.00  0.00 4.00 10.00 10.00  0.00
8.75 5.00 2.50 2.50 5.00  0.00 5.00 10.00 10.00  0.00
8.89 5.56 3.33 3.33 5.56  1.11 5.56 10.00 10.00  1.11
2.50 3.75 7.50 7.50 3.75 10.00 3.75  0.00  0.00 10.00
3.33 4.44 7.78 7.78 4.44 10.00 4.44  1.11  1.11 10.00
7.78 4.44 2.22 3.33 5.56  0.00 6.67  8.89 10.00  1.11
2.22 3.33 6.67 7.78 4.44  8.89 5.56  0.00  1.11 10.00
8.00 4.00 2.00 2.00 4.00  0.00 4.00 10.00 10.00  0.00
   x=: 3 _3 10 22 _8
   y=: _5 0 5 10 20 30
   x ([ , d12 , d11 ,: contour) y
3 _3 10 22 _8      x
3 _3 10 22 _5      x increased to not less than min contour level
0 _5 10 20 30      _8 mis-classified because less than lowest contour
0 _5 10 20 _5      Contour