Ranky
Compute rankings in Python.
Get started
pip install ranky
import ranky as rk
Read the documentation.
Main functions
The main functionalities include scoring metrics (e.g. accuracy, roc auc), rank metrics (e.g. Kendall Tau, Spearman correlation), ranking systems (e.g. Majority judgement, Kemeny-Young method) and some measurements (e.g. Kendall's W coefficient of concordance).
Most functions take as input 2-dimensional numpy.array or pandas.DataFrame objects. DataFrames are the best way to keep track of the names of each data point.
Let's consider the following preference matrix:
Each row is a candidate and each column is a judge. Here are the results of rk.average_rank(matrix), computing the mean rank of each candidate:
We can see that candidate2 has the best average rank among the four judges.
Let's display it using rk.show(rk.average_rank(matrix)):
In code:
import pandas as pd
import ranky as rk
m = pd.read_csv('data/matrix.csv', index_col=0) # candidates x judges
rk.average_rank(m) # Borda count, lower is better
rk.pairwise(m) # Copeland's method, higher is better
rk.kendall_w(m, axis=1) # agreement between judges
rk.show(rk.average_rank(m))
Conventions
- A preference matrix has one row per candidate and one column per judge. A judge can be a voter, a dataset, a metric, etc.
- By default, higher scores are better. Many functions have a
reverseargument to change this. - Ranking systems take an
axisargument, which is the axis of the judges (axis=1by default). Some lower-level functions (rk.rank,rk.consensus,rk.evolution_strategy,rk.brute_force) take the axis of the candidates instead. Check the docstrings when in doubt. - With a
pandas.DataFrameas input, the results keep the names of the candidates.
Ranking systems
The rank aggregation methods available include:
- Random Dictator:
rk.dictator(m) - Score Voting (mean):
rk.score(m) - Borda Count (average rank):
rk.average_rank(m)orrk.borda(m) - Majority Judgement (median):
rk.majority(m) - Uninominal Voting (multi-turn instant-runoff):
rk.uninominal(m, turns=2) - Pairwise methods. Copeland's method:
rk.pairwise(m)orrk.copeland(m), Success rate:rk.pairwise(m, wins=rk.success_rate)and more. You can specify your own "wins" function or select one from therk.duelmodule. - Optimal rank aggregation using any rank metric:
rk.center(m),rk.center(m, method='kendalltau'). Solver used [1]. - (Kemeny-Young method is optimal rank aggregation using Kendall's tau as metric:
rk.kemeny_young(m).) - (Optimal rank aggregation using Spearman correlation as metric is equivalent to Borda count.)
Metrics
You can use rk.any_metric(a, b, method) to call a metric from any of the three categories below.
-
Scoring metrics:
rk.metric(y_true, y_pred, method='accuracy'). Methods include:['accuracy', 'balanced_accuracy', 'precision', 'average_precision', 'f1_score', 'mxe', 'recall', 'jaccard', 'roc_auc', 'mse', 'rmse', 'mae', 'sar'] -
Rank correlation coefficients:
rk.corr(r1, r2, method='spearman'). Methods include:['kendalltau', 'spearman', 'pearson'] -
Rank distances:
rk.dist(r1, r2, method='levenshtein'). Methods include:['hamming', 'levenshtein', 'kendall', 'euclidean', 'winner', 'winner_mistake', 'winner_distance', 'symmetrical_winner_distance']
To add: general edit distances, kemeny distance, regression metrics...
Visualizations
- Use
rk.showto visualize preference matrix (2D) or ranking ballots (1D).
>>> rk.show(m)
>>> rk.show(m['judge1'])
- Use
rk.mdsto visualize (in 2D or 3D) the points in a given metric space. Seerk.scatterplotdocumentation for display arguments.
>>> rk.mds(m, method='euclidean')
>>> rk.mds(m, method='spearman', axis=1)
-
You can use
rk.tsnesimilarly tork.mds. -
Use
rk.critical_differenceto plot a critical difference diagram, comparing candidates' performance and grouping them by statistical equivalence. Such diagrams can be seen in [2, 3].
>>> rk.critical_difference(m, comparison_func=rk.bayes_wins)
- Show Condorcet graphs using
rk.show_graph(graph), based on [4]. The graph can be computed withrk.pairwise(m, return_graph=True).
Other
- Rank,
rk.rank, convert a 1D score ballot into a ranking. - Bootstrap,
rk.bootstrap, sample a given axis. - Consensus,
rk.consensus, check if rankings exactly agree. - Concordance,
rk.concordance, mean rank correlation between all judges of a preference matrix. - Centrality,
rk.centrality, mean rank correlation (or distance) between a ranking and a preference matrix. - Kendall's W,
rk.kendall_w, coefficient of concordance. - Judge generators,
rk.SwapGeneratorandrk.GaussianGenerator, sample noisy judges from a reference ranking. - Utility:
rk.read_codalab_csvto parse a CSV generated by Codalab representing a leaderboard into apandas.DataFrame.
Development
See CONTRIBUTING.md to run the tests, build the documentation and release a new version. The changes between versions are listed in CHANGELOG.md.
References
Please cite ranky in your publications if this is useful for your research. Here is an example BibTeX entry:
@misc{pavao2020ranky,
title={ranky},
author={Adrien Pavao},
year={2020},
howpublished={\url{https://github.com/didayolo/ranky}},
}
[1] Storn R. and Price K., Differential Evolution - a Simple and Efficient Heuristic for Global Optimization over Continuous Spaces, Journal of Global Optimization, 1997, 11, 341 - 359.
[2] Janez Demsar, Statistical Comparisons of Classifiers over Multiple Data Sets, 7(Jan):1--30, 2006.
[3] H. Ismail Fawaz, G. Forestier, J. Weber, L. Idoumghar, P. Muller, Deep learning for time series classification: a review, Data Mining and Knowledge Discovery, 2018.
[4] Aric A. Hagberg, Daniel A. Schult and Pieter J. Swart, “Exploring network structure, dynamics, and function using NetworkX”, in Proceedings of the 7th Python in Science Conference (SciPy2008), Gäel Varoquaux, Travis Vaught, and Jarrod Millman (Eds), (Pasadena, CA USA), pp. 11–15, Aug 2008.
License
Copyright (c) 2020-2021, Adrien PAVAO. This software is released under the Apache License 2.0 (the "License"); you may not use the software except in compliance with the License.
The text of the Apache License 2.0 can be found online at: http://www.opensource.org/licenses/apache2.0.php
Metadata
Release files for ranky 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ranky-1.0.0.tar.gz | 1.5 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ranky-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.5 MB
Release files / ranky-1.0.0.tar.gz
| Download URL | ranky-1.0.0.tar.gz |
|---|---|
| Size | 1.5 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fab38e44071bd8a05e88ab93d703785b2d0841fc8ce12750d0c5be361810c817
|
|
BLAKE2b-256 checksum How to use checksums |
a91c663d54589d238bc801f68415aa84813df9107336d2c8d345d09bbf3399dd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|
Release files / ranky-1.0.0-py3-none-any.whl
| Download URL | ranky-1.0.0-py3-none-any.whl |
|---|---|
| Size | 27.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9c6c2cb8e01de6abe787068e2fbdef631acf7bca71b317f0620924686a4c5956
|
|
BLAKE2b-256 checksum How to use checksums |
bba5c335769adbfdd0813a29d1d1150f2bec29440f6f6fa764178e43f7c85b75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.5
|