10000 GH-95861: Add support for Spearman's rank correlation coefficient by rhettinger · Pull Request #95863 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-95861: Add support for Spearman's rank correlation coefficient #95863

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

Merged
merged 9 commits into from
Aug 18, 2022
Prev Previous commit
Neaten up the docstring for _rank().
  • Loading branch information
rhettinger committed Aug 18, 2022
commit 873e24017c8d0b714dab869490b62f7f0bb5fe88
20 changes: 10 additions & 10 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,24 +358,24 @@ def _fail_neg(values, errmsg='negative value'):
def _rank(data, /, *, key=None, reverse=False, ties='average') -> list[float]:
"""Rank order a dataset. The lowest value has rank 1.

Ties are averaged so that equal values receive the same rank.
Ties are averaged so that equal values receive the same rank:

>>> data = [31, 56, 31, 25, 75, 18]
>>> _rank(data)
[3.5, 5.0, 3.5, 2.0, 6.0, 1.0]
>>> data = [31, 56, 31, 25, 75, 18]
>>> _rank(data)
[3.5, 5.0, 3.5, 2.0, 6.0, 1.0]

The operation is idempotent.
The operation is idempotent:

>>> _rank([3.5, 5.0, 3.5, 2.0, 6.0, 1.0])
[3.5, 5.0, 3.5, 2.0, 6.0, 1.0]
>>> _rank([3.5, 5.0, 3.5, 2.0, 6.0, 1.0])
[3.5, 5.0, 3.5, 2.0, 6.0, 1.0]

It is possible to rank the data in reverse order so that
the highest value has rank 1. Also, a key-function can
extract the field to be ranked:

>>> goals = [('eagles', 45), ('bears', 48), ('lions', 44)]
>>> _rank(goals, key=itemgetter(1), reverse=True)
[2.0, 1.0, 3.0]
>>> goals = [('eagles', 45), ('bears', 48), ('lions', 44)]
>>> _rank(goals, key=itemgetter(1), reverse=True)
[2.0, 1.0, 3.0]

"""
# If this function becomes public at some point, more thought
Expand Down
0