-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance ranking code #589
Enhance ranking code #589
Changes from 3 commits
ef2ba05
3848eff
d050fdb
8a296ba
831a0e5
88aeee5
c842c07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,12 +24,9 @@ function _rank(f!, x::AbstractArray{>: Missing}, R::Type=Int; sortkwargs...) | |||||
inds = findall(!ismissing, vec(x)) | ||||||
isempty(inds) && return missings(R, size(x)) | ||||||
T = nonmissingtype(eltype(x)) | ||||||
xv = Vector{T}(undef, length(inds)) | ||||||
@inbounds for (i, ind) in enumerate(inds) | ||||||
xv[i] = x[ind] | ||||||
end | ||||||
rks = missings(R, size(x)) | ||||||
xv = disallowmissing(view(vec(x), inds)) | ||||||
ordv = sortperm(xv; sortkwargs...) | ||||||
rks = missings(R, size(x)) | ||||||
f!(view(rks, inds), xv, ordv) | ||||||
return rks | ||||||
end | ||||||
|
@@ -45,12 +42,11 @@ end | |||||
|
||||||
|
||||||
""" | ||||||
ordinalrank(x; sortkwargs...) | ||||||
ordinalrank(x; lt=isless, by=identity, rev::Bool=false, ...) | ||||||
|
||||||
Return the [ordinal ranking](https://en.wikipedia.org/wiki/Ranking#Ordinal_ranking_.28.221234.22_ranking.29) | ||||||
("1234" ranking) of an array. The `lt` keyword allows providing a custom "less | ||||||
than" function; use `rev=true` to reverse the sorting order. | ||||||
All items in `x` are given distinct, successive ranks based on their | ||||||
("1234" ranking) of an array. Supports the same keyword arguments as `sort(x; sortkwargs...)` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, in L48 I sort-of introduce the sortkwargs, because otherwise later in the docstring it would not be entirely clear that the keyword args are passed through to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. I would just have said "in the sorted vector" in the next sentence. As you prefer, but if you keep this then please replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that sounds good. I also simplified a bit the language in the other ranking docstrings. |
||||||
function. All items in `x` are given distinct, successive ranks based on their | ||||||
position in `sort(x; sortkwargs...)`. | ||||||
Missing values are assigned rank `missing`. | ||||||
""" | ||||||
|
@@ -67,17 +63,14 @@ function _competerank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray) | |||||
v = x[p1] | ||||||
rks[p1] = k = 1 | ||||||
|
||||||
i = 2 | ||||||
while i <= n | ||||||
for i in 2:n | ||||||
pi = p[i] | ||||||
xi = x[pi] | ||||||
if xi == v | ||||||
rks[pi] = k | ||||||
else | ||||||
rks[pi] = k = i | ||||||
if xi != v | ||||||
v = xi | ||||||
k = i | ||||||
end | ||||||
i += 1 | ||||||
rks[pi] = k | ||||||
end | ||||||
end | ||||||
|
||||||
|
@@ -86,11 +79,10 @@ end | |||||
|
||||||
|
||||||
""" | ||||||
competerank(x; sortkwargs...) | ||||||
competerank(x; lt=isless, by=identity, rev::Bool=false, ...) | ||||||
|
||||||
Return the [standard competition ranking](http://en.wikipedia.org/wiki/Ranking#Standard_competition_ranking_.28.221224.22_ranking.29) | ||||||
("1224" ranking) of an array. The `lt` keyword allows providing a custom "less | ||||||
than" function; use `rev=true` to reverse the sorting order. | ||||||
("1224" ranking) of an array. Supports the same keyword arguments as `sort(x)` function. | ||||||
Items that compare equal are given the same rank, then a gap is left | ||||||
in the rankings the size of the number of tied items - 1. | ||||||
Missing values are assigned rank `missing`. | ||||||
|
@@ -108,17 +100,14 @@ function _denserank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray) | |||||
v = x[p1] | ||||||
rks[p1] = k = 1 | ||||||
|
||||||
i = 2 | ||||||
while i <= n | ||||||
for i in 2:n | ||||||
pi = p[i] | ||||||
xi = x[pi] | ||||||
if xi == v | ||||||
rks[pi] = k | ||||||
else | ||||||
rks[pi] = (k += 1) | ||||||
if xi != v | ||||||
v = xi | ||||||
k += 1 | ||||||
end | ||||||
i += 1 | ||||||
rks[pi] = k | ||||||
end | ||||||
end | ||||||
|
||||||
|
@@ -127,12 +116,11 @@ end | |||||
|
||||||
|
||||||
""" | ||||||
denserank(x; sortkwargs...) | ||||||
denserank(x; lt=isless, by=identity, rev::Bool=false, ...) | ||||||
|
||||||
Return the [dense ranking](http://en.wikipedia.org/wiki/Ranking#Dense_ranking_.28.221223.22_ranking.29) | ||||||
("1223" ranking) of an array. The `lt` keyword allows providing a custom "less | ||||||
than" function; use `rev=true` to reverse the sorting order. Items that | ||||||
compare equal receive the same ranking, and the next subsequent rank is | ||||||
("1223" ranking) of an array. Supports the same keyword arguments as `sort(x)` function. | ||||||
Items that compare equal receive the same ranking, and the next subsequent rank is | ||||||
assigned with no gap. | ||||||
Missing values are assigned rank `missing`. | ||||||
""" | ||||||
|
@@ -148,8 +136,7 @@ function _tiedrank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray) | |||||
v = x[p[1]] | ||||||
|
||||||
s = 1 # starting index of current range | ||||||
e = 2 # pass-by-end index of current range | ||||||
while e <= n | ||||||
for e in 2:n # e is pass-by-end index of current range | ||||||
cx = x[p[e]] | ||||||
if cx != v | ||||||
# fill average rank to s : e-1 | ||||||
|
@@ -161,10 +148,9 @@ function _tiedrank!(rks::AbstractArray, x::AbstractArray, p::IntegerArray) | |||||
s = e | ||||||
v = cx | ||||||
end | ||||||
e += 1 | ||||||
end | ||||||
|
||||||
# the last range (e == n+1) | ||||||
# the last range | ||||||
ar = (s + n) / 2 | ||||||
for i = s : n | ||||||
rks[p[i]] = ar | ||||||
|
@@ -176,12 +162,11 @@ end | |||||
|
||||||
# order (aka. rank), resolving ties using the mean rank | ||||||
""" | ||||||
tiedrank(x; sortkwargs...) | ||||||
tiedrank(x; lt=isless, by=identity, rev::Bool=false, ...) | ||||||
|
||||||
Return the [tied ranking](http://en.wikipedia.org/wiki/Ranking#Fractional_ranking_.28.221_2.5_2.5_4.22_ranking.29), | ||||||
also called fractional or "1 2.5 2.5 4" ranking, | ||||||
of an array. The `lt` keyword allows providing a custom "less | ||||||
than" function; use `rev=true` to reverse the sorting order. | ||||||
of an array. Supports the same keyword arguments as `sort(x)` function. | ||||||
Items that compare equal receive the mean of the | ||||||
rankings they would have been assigned under ordinal ranking. | ||||||
Missing values are assigned rank `missing`. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just noticed that
T
is not used anymore, will remove this line