milia.recommend.ranking#
Ranking cases, groups and models, with uncertainty on the ranks.
weights() is the single diversity weighting used everywhere a ranking
combines several metrics (cluster-count selection and model comparison alike):
a metric that mostly repeats others gets less weight.
Module Attributes
Functions
|
Combine columns on different scales: min-max each, then take the max or the mean. |
|
Aggregate cases to groups (e.g. buses, feeders), combine the aggregates and rank the groups. |
|
|
|
Diversity-weighted multi-metric ranking of models. |
|
Sort by a score column and prepend a |
|
Diversity weights of metric columns from their mean absolute Spearman correlation. |
- milia.recommend.ranking.weights(scores)#
Diversity weights of metric columns from their mean absolute Spearman correlation.
Each metric’s weight is one minus its mean absolute correlation with the others, floored at 0.05 and normalised to sum to one, so a metric that mostly repeats others counts less.
- Parameters:
scores (
DataFrame) – One column per metric, oriented higher-is-better, one row per candidate.- Return type:
- Returns:
One weight per column.
Examples
>>> import pandas as pd >>> scores = pd.DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [3, 1, 2]}) >>> weights(scores).round(2).tolist() [0.3, 0.3, 0.4]
- milia.recommend.ranking.combine(frame, columns, method='max')#
Combine columns on different scales: min-max each, then take the max or the mean.
- Parameters:
- Return type:
- Returns:
One combined score per row, on
[0, 1].- Raises:
ValueError – If the method is unknown.
Examples
>>> import pandas as pd >>> combine(pd.DataFrame({"v": [1.0, 3.0], "w": [10.0, 0.0]}), ["v", "w"]).tolist() [1.0, 1.0]
- milia.recommend.ranking.rank(frame, score, ascending=False)#
Sort by a score column and prepend a
rankcolumn (1 = top).- Parameters:
- Return type:
- Returns:
The sorted frame with
rank.
Examples
>>> import pandas as pd >>> rank(pd.DataFrame({"name": ["a", "b"], "s": [1.0, 2.0]}), "s")["name"].tolist() ['b', 'a']
- milia.recommend.ranking.interval(frame, score, ascending=False, bootstrap=200, random_state=0)#
rank()plus a bootstrap interval on each rank.The cases are resampled with replacement and re-ranked; the 5th and 95th percentiles of each case’s positions give
lowerandupper. Wide intervals mean the order among those cases is not settled.- Parameters:
- Return type:
- Returns:
The ranked frame with
lowerandupper.
Examples
>>> import pandas as pd >>> ranked = interval(pd.DataFrame({"s": [1.0, 2.0, 3.0]}), "s", bootstrap=50) >>> bool((ranked["lower"] <= ranked["rank"]).all()) True
- milia.recommend.ranking.groups(frame, by, agg, score=None)#
Aggregate cases to groups (e.g. buses, feeders), combine the aggregates and rank the groups.
- Parameters:
- Return type:
- Returns:
One row per group with the aggregates,
scoreandrank.
Examples
>>> import pandas as pd >>> frame = pd.DataFrame({"bus": [1, 1, 2], "v": [1.0, 1.2, 1.05]}) >>> groups(frame, "bus", {"v": "max"})["bus"].tolist() [1, 2]
- milia.recommend.ranking.LOWER = frozenset({'mae', 'mape', 'mse', 'nrmse', 'rmse', 'smape', 'wmape'})#
Metric names treated as lower-is-better by
models().
- milia.recommend.ranking.models(scores, lower=None)#
Diversity-weighted multi-metric ranking of models.
Metrics named in
lowerare negated so higher is better throughout; each metric is ranked across models and combined three ways: mean rank, Borda count, and Borda weighted byweights().- Parameters:
- Return type:
- Returns:
scoreswithmean,bordaandweighted, best first; the weights are inattrs["weights"].
Examples
>>> import pandas as pd >>> scores = pd.DataFrame({"mae": [1.0, 2.0], "corr": [0.9, 0.5]}, index=["knn", "mean"]) >>> models(scores).index.tolist() ['knn', 'mean']