Skip to content

Instantly share code, notes, and snippets.

@leriomaggio
Last active December 16, 2022 16:42
Show Gist options
  • Save leriomaggio/d85bf91b8f3ed11b7927eaf39bdc79ae to your computer and use it in GitHub Desktop.
Save leriomaggio/d85bf91b8f3ed11b7927eaf39bdc79ae to your computer and use it in GitHub Desktop.
Numpy based implementation of the Borda Selection Algorithm for Rankings
"""Python/Numpy implementation of the Borda Count"""
import numpy as np
from nptyping import NDArray
from typing import Tuple, Union, List, Any
RankingsList = Union[NDArray[(Any, Any), np.int], List[List[int]]]
Ranking = NDArray[(Any,), np.int]
Counts = NDArray[(Any,), np.int]
AveragePositions = NDArray[(Any,), np.float]
Borda = Tuple[Ranking, Counts, AveragePositions]
def borda_count_np(X: RankingsList, min_score: float = 1, k: int = None) -> Borda:
"""Given N ranked ids lists of length P compute the number of
extractions on top-k positions and the mean position for each id.
Sort the element ids (in reversed DECREASING order) based on the
Borda Extraction Scores (BES). IDs having the same BES will be sorted
by their increasing average position (i.e. the average of the position
of each ID in all the rankings).
Parameters
----------
X : Numpy array of integer with shape [N, P] or List of Lists
The list of rankings
min_score: float, optional
Minimum score considered for IDs (Default is 1)
k : int, optional
Top-K positions to select.
Default is None, which means that all the positions will be
selected (i.e. k = P)
Returns
-------
Borda
A tuple containing three NDarray:
1. Sorted IDs, i.e. the Borda Ranking;
2. The number of Extractions, i.e. Count;
3. The average positions of each ID.
Raises
------
ValueError
If k < 1 or k > p
ValueError
If X contains duplicate IDs
"""
x_np = np.asarray(X) # make sure X is a Numpy array for efficiency
n, p = x_np.shape
if k is None:
k = p
if not 1 <= k <= p:
raise ValueError(f"k must be in [1, {p}], {k} given instead!")
# Verify that ranked lists do no contain repetitions
unique_ids = np.asarray([len(set(r)) for r in x_np[:]])
if len(unique_ids[unique_ids < p]):
raise ValueError(
f"IDs in list of rankings are not unique! Unique IDs found: {unique_ids}"
)
positions = np.argsort(x_np, axis=1)
# Select TopK by forcing positions of others to last position
positions[positions >= k] = p
# Calculate scores of extractions in each ranking based on their relative position
exts = (p - positions - 1 + min_score).sum(axis=0)
# Count total number of valid extractions
counts = (positions < k).sum(axis=0)
# Reset all non-valid positions (>=k) for TopK selection
positions[positions >= k] = 0
# Calculate Average positions of each element in each ranking
non_zero_counts = counts != 0
avg_positions = positions.sum(axis=0) / np.where(non_zero_counts, counts, 1)
avg_positions[~non_zero_counts] = p - 1
# Sort rankings based on average positions, and extraction scores
idx = np.lexsort((avg_positions, exts))[::-1]
return idx, counts[idx], avg_positions[idx]
f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 f19 f20 f21 f22 f23 f24 f25 f26 f27 f28 f29 f30 f31 f32 f33 f34 f35 f36 f37 f38 f39 f40 f41 f42 f43 f44 f45 f46 f47 f48 f49 f50 f51 f52 f53 f54 f55 f56 f57 f58 f59 f60 f61 f62 f63 f64 f65 f66 f67 f68 f69 f70 f71 f72 f73 f74 f75 f76 f77 f78 f79 f80 f81 f82 f83 f84 f85 f86 f87 f88 f89 f90 f91 f92 f93 f94 f95 f96 f97 f98 f99 f100 f101 f102 f103 f104 f105 f106 f107
0 73 5 85 42 64 1 32 14 84 79 57 91 8 40 58 3 47 17 41 21 7 16 13 88 72 23 12 74 56 36 19 89 30 2 4 31 9 29 65 78 93 105 46 63 71 15 20 0 94 37 43 52 62 80 33 102 55 86 53 51 103 24 95 25 44 106 101 22 87 76 27 35 28 81 82 66 11 38 6 104 90 69 26 83 75 18 54 70 48 59 61 67 10 50 77 96 97 99 100 98 92 68 39 34 49 45 60
1 5 85 73 32 64 42 1 23 8 14 58 21 47 17 41 40 91 3 57 79 7 12 84 31 74 37 13 16 19 4 36 15 30 63 71 9 62 20 52 93 55 29 51 0 88 56 89 98 6 102 92 78 81 82 86 72 11 2 46 90 105 33 53 44 97 106 101 104 100 24 35 25 94 38 27 28 96 65 66 87 76 22 103 69 83 54 75 18 70 48 59 61 10 67 50 43 95 80 77 26 99 68 60 45 34 39 49
2 5 73 85 1 42 64 57 16 79 14 32 84 58 40 3 8 47 17 41 21 78 74 35 36 38 91 7 12 46 56 88 76 87 13 19 23 93 4 31 2 63 71 62 52 55 94 72 20 9 51 89 30 37 0 15 66 69 28 102 27 67 25 33 86 53 65 29 60 105 49 44 24 68 77 45 22 43 95 103 81 106 101 82 80 26 34 39 104 96 6 11 97 98 90 100 50 54 61 59 70 48 18 75 99 92 83 10
3 73 85 5 79 42 32 1 84 64 63 71 14 52 8 23 58 21 7 62 47 17 13 41 91 57 40 3 31 55 12 16 74 0 51 93 37 88 36 9 4 102 19 15 30 105 89 56 78 29 20 72 2 94 46 95 6 86 106 101 33 53 65 90 11 44 81 104 25 82 24 27 38 35 28 43 66 103 76 87 69 80 22 99 97 92 67 98 100 54 83 75 18 70 48 59 61 10 26 50 77 96 39 34 45 49 68 60
4 85 73 5 32 42 1 64 23 84 79 58 8 14 74 21 47 17 41 19 40 57 3 7 37 13 91 31 16 12 63 20 71 52 36 4 62 0 9 89 93 55 51 88 15 94 56 105 30 102 78 72 81 82 2 46 86 33 29 53 95 25 24 44 38 35 106 101 27 22 103 65 28 90 66 104 26 11 69 43 54 76 87 83 18 75 70 48 59 61 96 6 67 80 10 50 99 97 98 100 68 45 60 49 92 77 39 34
5 85 5 73 42 1 64 32 14 8 58 57 21 47 17 23 41 40 84 91 79 3 7 13 16 12 74 31 19 37 36 4 93 63 71 52 9 0 30 88 94 62 56 20 81 82 15 89 51 55 29 102 105 33 46 78 2 86 72 95 53 44 25 24 27 28 35 11 106 101 6 90 66 38 103 65 22 104 69 76 87 67 83 92 75 54 18 48 70 59 61 96 10 50 43 97 98 100 80 26 77 49 45 68 99 34 39 60
6 73 5 85 32 1 42 64 58 8 21 14 47 17 41 91 23 79 84 40 57 74 3 7 93 13 12 19 16 31 30 63 36 71 29 52 4 102 88 62 37 9 56 15 89 0 72 20 51 2 94 78 55 105 46 6 65 95 86 33 11 53 106 101 24 43 25 90 44 104 22 27 38 35 80 28 87 76 66 69 81 82 103 26 83 54 99 18 75 92 70 48 59 61 67 97 98 50 10 96 100 77 68 45 49 60 39 34
7 73 5 85 64 42 1 32 14 58 8 47 17 21 41 40 57 79 3 84 7 91 13 23 12 16 74 36 4 19 63 31 20 9 71 93 52 62 88 37 56 30 0 78 51 15 102 55 72 2 89 29 46 105 86 33 53 44 65 81 94 82 24 25 35 27 87 76 28 38 106 101 103 43 6 11 66 22 90 80 69 95 104 67 54 83 18 75 70 48 59 97 61 26 10 98 100 50 92 96 77 99 49 45 68 39 34 60
8 73 5 85 42 1 32 64 74 14 8 58 84 40 79 47 17 41 57 21 3 16 91 19 7 23 12 13 78 36 31 88 72 4 35 89 56 38 2 46 62 20 63 71 9 93 52 30 51 37 55 65 0 94 76 87 15 29 43 102 86 25 105 53 27 66 24 28 33 69 67 44 80 95 22 60 68 49 26 45 90 6 11 106 101 81 82 104 103 77 83 54 18 75 70 48 59 61 96 50 10 98 97 92 100 99 34 39
9 73 5 79 85 84 32 42 64 1 14 57 8 23 40 3 58 21 47 17 71 41 63 55 37 7 16 52 31 13 91 12 62 15 74 36 19 4 88 0 9 51 56 89 20 34 39 78 105 93 72 2 46 103 50 30 106 101 86 53 33 94 102 98 61 44 81 59 48 29 70 82 18 75 65 35 38 104 25 83 24 97 87 76 27 54 28 100 66 43 6 69 95 22 10 90 67 96 80 11 77 92 26 68 49 45 60 99
10 73 85 5 32 42 64 84 1 79 14 58 8 74 47 17 40 41 21 57 19 23 3 91 7 13 16 12 31 37 36 93 4 20 88 71 63 55 89 94 56 9 15 52 62 72 102 30 0 2 46 105 29 78 95 51 86 33 65 53 24 25 81 82 22 106 101 44 43 35 26 27 28 104 80 87 76 66 38 103 90 11 69 54 83 18 75 70 48 59 96 61 6 97 98 10 50 67 100 99 92 68 60 77 45 39 34 49
11 5 85 73 32 42 64 1 58 21 79 84 47 17 41 8 14 23 57 40 3 91 74 7 12 31 16 19 13 37 63 71 36 4 52 62 20 93 55 30 9 51 88 0 29 56 78 15 89 94 102 81 105 6 82 33 86 46 2 53 72 44 11 25 24 90 27 35 28 38 103 66 95 65 87 76 106 101 69 92 22 104 67 43 96 54 83 75 18 70 48 59 61 97 10 77 80 50 100 98 99 26 34 39 45 49 68 60
12 73 5 85 1 64 42 32 8 58 14 47 17 41 40 21 91 3 57 74 16 23 7 79 84 12 13 36 19 31 4 62 93 63 30 71 78 37 52 35 38 51 9 29 88 20 0 56 55 89 46 2 15 72 102 86 94 53 44 66 27 105 28 25 69 67 76 87 33 24 6 60 11 68 49 45 65 22 81 82 90 103 106 101 43 95 92 34 39 77 104 96 26 80 98 97 100 10 50 54 61 59 70 48 18 83 75 99
13 5 73 85 32 42 64 1 79 84 14 58 8 57 21 23 47 17 40 41 3 7 91 63 16 13 71 52 31 62 74 12 88 19 51 36 89 56 78 37 4 0 55 9 15 72 20 2 105 30 46 93 102 29 65 94 95 81 82 86 33 106 101 53 87 76 38 44 35 43 103 25 24 6 27 80 104 90 11 28 66 22 69 99 83 97 54 75 18 70 48 59 67 61 100 10 50 98 77 26 92 96 68 45 49 60 39 34
14 5 73 85 42 64 1 14 32 79 57 84 40 7 3 8 13 58 16 47 17 21 41 15 91 12 23 56 88 36 9 74 31 4 63 71 72 93 2 52 0 46 19 37 78 89 62 55 98 30 102 65 94 20 106 101 105 51 104 29 33 86 43 53 97 44 25 87 76 100 38 24 35 80 27 103 28 66 95 22 69 90 6 11 81 82 96 67 26 83 77 54 75 18 70 48 59 61 10 50 92 49 45 68 99 34 39 60
15 73 85 5 32 42 79 84 64 1 58 21 47 17 41 8 14 23 63 57 40 71 74 3 91 52 62 31 7 55 12 16 13 19 51 37 36 4 93 88 20 9 0 30 56 15 78 89 102 94 29 105 81 2 72 82 46 86 33 53 6 44 38 35 103 24 25 27 95 106 101 28 39 34 66 65 11 90 22 69 104 92 76 87 67 96 43 50 61 97 59 48 70 99 18 75 54 83 80 26 100 98 77 10 68 45 49 60
16 5 73 85 32 42 1 64 14 79 84 8 40 57 23 3 21 58 16 47 17 41 31 91 7 93 13 78 12 74 15 36 19 63 71 88 35 20 62 4 52 37 56 98 102 46 38 55 9 2 72 51 89 0 87 76 30 94 104 106 101 29 97 100 65 90 81 82 43 6 105 11 66 69 67 80 28 86 27 25 33 53 60 68 49 95 44 96 45 24 54 77 83 18 70 75 48 59 61 10 92 50 22 103 99 26 39 34
17 5 73 85 64 42 1 57 14 32 84 79 8 40 3 58 47 17 21 41 16 91 7 23 88 74 36 12 13 19 56 4 31 93 37 63 71 89 9 52 30 105 2 62 15 102 46 78 33 20 72 86 51 94 53 29 55 0 44 25 65 24 27 28 95 35 106 101 66 22 87 76 38 81 82 103 69 43 104 67 26 80 6 11 97 77 100 98 90 99 83 54 75 18 70 48 59 61 96 39 34 50 49 45 10 92 60 68
18 73 5 85 1 42 64 32 14 79 84 8 58 57 40 47 17 7 41 21 3 13 16 74 91 23 12 36 19 63 71 88 0 9 89 4 52 31 56 37 72 62 2 78 20 93 105 15 55 30 46 51 65 86 33 103 53 29 94 24 25 44 102 43 22 27 28 35 80 38 66 87 76 106 101 95 69 81 26 82 67 77 83 11 104 75 18 48 59 70 61 10 54 50 90 96 6 97 98 99 100 49 45 92 68 39 34 60
19 73 85 5 32 42 64 1 91 58 8 21 23 14 47 17 41 74 40 79 84 7 57 3 19 13 12 31 16 4 37 30 36 9 71 29 63 52 0 62 20 93 55 94 6 15 88 89 51 56 72 11 105 90 78 102 2 46 92 86 95 81 33 82 53 44 24 25 65 106 101 35 22 27 38 87 76 28 104 103 43 66 83 54 96 75 18 70 48 59 61 10 69 80 50 26 67 98 97 100 99 77 68 60 34 39 49 45
20 32 85 5 73 42 23 1 64 79 84 14 21 8 58 31 47 17 7 41 37 13 91 40 57 3 0 74 19 12 63 71 16 52 93 9 4 72 88 36 62 55 20 102 56 30 89 15 105 2 51 29 94 46 65 78 81 82 11 43 90 95 80 6 33 103 86 106 101 53 10 83 25 75 18 48 59 70 61 44 54 24 50 35 38 104 27 28 99 66 22 87 76 77 97 69 98 100 67 92 34 39 26 96 68 60 45 49
21 5 85 73 64 32 42 1 14 58 8 57 47 17 21 41 91 40 3 23 84 74 79 16 7 13 19 12 36 93 31 4 30 9 37 88 20 29 94 56 78 15 33 0 63 62 71 89 102 52 105 86 51 46 2 44 53 81 82 55 25 24 72 27 28 35 66 38 95 22 87 76 92 106 101 69 65 6 11 103 90 67 104 83 96 54 75 18 26 70 48 59 61 43 50 10 98 77 97 80 100 49 45 99 68 60 39 34
22 73 79 5 84 85 42 1 64 32 91 58 14 21 8 47 74 17 41 63 71 19 23 7 52 57 40 15 3 62 13 55 94 12 31 93 20 16 51 88 37 4 30 0 89 36 56 72 9 29 78 106 101 102 2 104 98 46 6 65 95 100 105 97 43 90 11 86 53 33 24 80 44 25 87 76 22 35 38 96 27 28 26 66 103 81 82 92 69 54 83 18 75 70 48 59 61 67 50 10 34 39 68 77 60 99 49 45
23 5 73 85 64 1 42 57 16 14 40 3 32 8 58 47 17 41 21 35 36 78 79 84 7 38 23 13 12 74 91 31 19 4 46 56 88 37 89 76 87 63 71 62 9 2 52 20 93 51 72 0 15 55 30 66 69 67 28 27 86 102 33 25 53 105 60 44 49 68 81 65 82 24 94 45 29 43 22 106 101 103 80 95 104 77 90 11 54 83 26 75 18 70 48 59 61 10 97 98 6 96 50 100 99 34 39 92
24 73 5 85 64 42 1 79 32 84 58 14 8 47 17 21 41 40 57 3 91 12 7 63 71 16 74 13 23 52 4 36 62 19 55 31 88 9 37 51 30 93 56 0 20 15 89 78 105 29 72 2 46 86 53 102 33 44 94 24 25 65 103 6 27 35 38 28 106 101 66 81 22 82 87 76 95 34 39 43 69 11 104 67 90 80 92 26 96 97 50 61 98 59 70 48 54 18 75 100 83 77 10 45 49 68 99 60
25 73 5 85 32 64 42 1 79 84 63 14 71 52 8 58 23 62 40 21 57 3 47 17 41 7 13 91 16 12 74 51 31 55 88 93 36 37 19 4 9 0 78 56 20 94 72 89 2 15 30 46 102 105 29 65 86 53 33 44 6 103 43 95 24 25 87 76 106 101 90 38 35 27 81 11 82 28 80 66 39 34 22 104 96 69 92 67 50 98 26 97 100 61 59 48 70 18 75 77 99 83 54 68 45 49 10 60
26 73 5 85 1 64 42 32 14 8 58 91 47 17 57 40 41 21 3 16 84 79 74 15 7 23 12 78 19 13 36 35 31 30 4 38 37 88 93 29 9 20 62 56 106 101 46 102 63 104 71 89 51 76 87 52 98 2 94 0 55 105 97 72 100 66 27 28 69 86 67 33 81 25 95 53 82 44 60 24 68 49 6 45 65 11 90 22 43 77 92 103 83 54 80 75 18 70 48 59 61 26 50 10 99 96 34 39
27 73 85 5 84 79 42 64 1 32 58 14 47 17 8 57 41 21 74 40 3 91 19 16 7 13 23 12 71 63 36 31 52 55 88 4 20 37 62 9 56 0 30 93 89 94 105 78 15 29 51 2 46 33 72 103 86 53 81 82 25 24 44 102 27 35 28 65 38 22 95 66 106 101 76 87 69 6 26 43 67 11 90 104 96 80 54 83 18 75 70 48 59 61 99 50 97 39 34 10 98 100 68 49 77 92 45 60
28 5 73 85 32 64 42 1 14 57 8 40 79 58 3 23 21 84 47 17 41 16 7 91 13 12 37 31 36 74 4 63 19 71 62 52 9 89 56 51 0 88 55 20 105 30 15 46 2 93 78 72 102 33 86 29 44 53 81 82 65 35 38 103 25 24 27 94 28 106 101 66 90 87 76 6 95 11 43 69 22 104 67 80 83 97 54 75 18 70 48 59 92 99 61 100 98 10 50 26 77 96 45 49 68 34 39 60
29 85 73 5 32 42 1 64 58 23 8 21 47 17 41 14 93 7 74 40 79 91 84 57 13 3 19 31 12 16 37 4 36 72 0 9 88 102 20 30 56 89 2 94 63 71 29 52 15 62 65 46 78 55 43 51 33 86 105 11 80 53 24 25 44 22 90 6 95 27 87 76 35 106 101 10 28 77 83 81 75 18 104 48 59 61 70 50 26 82 66 38 54 96 69 98 67 97 92 103 100 39 34 68 99 60 49 45
30 5 73 85 42 64 32 1 14 57 58 84 8 79 19 40 74 47 17 21 41 20 3 23 16 7 91 13 56 12 31 88 36 93 37 4 89 78 46 63 71 72 9 2 102 52 30 62 55 15 29 0 105 94 51 65 81 82 33 86 95 53 43 25 106 101 24 87 76 35 80 22 38 11 44 90 104 6 27 26 28 66 54 103 83 18 70 48 59 75 61 99 97 10 69 98 50 100 67 96 77 68 34 39 60 92 45 49
31 73 5 85 64 42 1 32 79 84 14 8 58 40 47 17 41 21 3 91 57 7 15 23 13 12 16 74 63 4 71 36 52 31 19 62 9 55 37 93 30 88 56 0 51 106 101 29 78 102 98 89 104 20 94 72 2 105 46 97 86 100 53 33 44 95 24 65 6 25 27 22 35 28 87 76 38 11 43 90 66 81 82 39 34 103 69 92 80 67 26 50 96 99 61 59 70 48 18 54 75 77 83 45 49 68 10 60
32 73 5 85 84 1 79 64 14 57 3 16 42 40 8 58 38 32 47 17 41 91 21 36 35 74 7 13 12 93 23 19 4 62 88 78 30 63 31 71 52 45 68 51 9 67 56 49 60 66 69 46 55 28 27 102 37 15 86 2 33 20 94 29 53 25 89 44 76 87 105 0 24 72 65 22 95 106 101 6 104 43 81 103 82 26 11 80 97 77 90 96 98 100 92 39 34 83 99 54 75 18 70 48 59 61 50 10
33 85 32 5 73 42 1 64 23 58 21 47 8 17 41 14 91 79 57 40 84 7 3 37 31 13 74 12 19 16 0 4 36 63 71 20 9 88 52 89 56 62 72 30 105 55 78 51 2 81 93 15 82 103 46 29 65 94 11 102 33 90 86 43 53 6 35 44 38 10 80 87 76 25 83 75 18 48 54 70 59 24 61 27 50 28 106 101 95 66 77 22 69 96 67 104 34 39 97 99 92 100 68 26 98 60 49 45
34 85 73 5 32 42 64 1 79 14 23 84 63 8 71 21 58 52 47 17 40 41 57 7 3 62 13 91 31 12 16 37 74 93 51 0 36 4 55 88 19 9 89 56 30 94 78 15 72 2 20 46 102 29 105 86 33 53 81 82 44 65 25 24 90 35 27 28 38 6 66 87 76 103 11 43 95 96 106 101 92 69 22 80 67 104 83 54 75 18 70 48 59 61 50 77 10 98 99 26 97 100 45 49 68 39 34 60
35 73 5 85 42 64 79 84 32 1 14 57 40 3 8 58 47 17 21 16 41 23 7 36 74 15 13 31 12 91 19 37 88 71 63 4 93 55 56 52 9 62 33 78 2 20 46 86 89 72 106 101 53 94 102 0 30 25 51 44 105 104 24 98 27 28 65 66 29 35 97 38 76 87 22 100 69 81 82 95 43 67 103 34 39 96 26 80 77 50 61 59 70 48 54 18 75 83 6 49 99 92 45 90 11 10 60 68
36 73 5 85 42 64 14 1 79 32 84 57 3 40 58 8 16 47 17 41 21 63 91 71 36 7 12 78 52 74 13 62 23 35 38 4 88 31 19 55 51 9 56 37 46 2 30 0 76 87 20 89 72 103 86 105 93 66 53 27 69 28 44 67 15 33 29 25 24 65 60 94 49 45 68 102 22 43 81 82 6 106 101 11 95 90 80 77 96 26 92 39 34 104 54 83 75 18 70 48 59 61 50 97 99 98 100 10
37 85 73 5 42 32 64 1 79 84 14 8 58 21 23 57 47 40 17 41 7 3 13 63 91 71 74 52 62 19 16 31 88 12 93 51 0 37 36 78 4 9 94 55 30 56 89 20 72 29 2 15 102 46 105 6 81 65 82 95 11 86 33 90 43 53 87 76 38 35 44 106 101 25 24 80 27 28 104 103 66 22 92 54 83 18 75 70 48 59 96 61 69 50 10 98 97 67 26 100 77 68 60 45 99 39 34 49
38 5 73 85 1 32 64 42 58 8 14 23 91 47 17 21 41 57 40 3 84 79 74 7 19 16 12 13 37 31 20 93 36 4 102 30 89 9 56 29 15 105 63 71 0 88 52 62 55 72 81 46 82 2 51 94 33 86 11 6 78 53 106 101 24 90 44 25 95 35 104 65 22 27 28 38 66 99 103 87 76 43 54 97 83 80 18 75 70 10 48 59 98 69 61 100 26 50 67 92 96 34 39 68 77 49 45 60
39 73 5 85 32 1 42 64 58 21 8 91 47 17 41 14 23 7 79 84 13 74 40 57 3 31 12 19 0 16 63 37 4 71 93 52 9 62 36 88 30 89 20 51 72 56 15 94 29 55 2 102 78 46 105 65 11 90 95 6 43 86 106 101 53 33 80 44 24 81 25 82 104 10 35 38 83 87 76 75 18 48 70 59 54 61 22 27 50 28 103 66 69 99 97 92 77 96 26 98 100 67 34 39 68 60 45 49
40 85 73 5 1 32 42 64 8 14 58 40 15 47 17 21 74 41 3 57 16 23 7 79 84 13 19 91 12 36 31 106 101 4 37 35 78 104 38 88 98 97 72 9 89 100 102 2 56 63 0 71 62 105 20 52 46 30 51 93 55 65 95 29 76 87 86 66 25 27 43 53 28 94 69 33 67 44 24 81 82 60 80 49 68 45 22 99 11 77 90 103 6 26 83 75 18 48 70 59 54 61 10 50 96 92 39 34
41 73 85 5 42 32 64 1 14 84 79 91 57 8 58 40 47 21 17 23 41 3 7 16 74 31 13 19 20 12 30 36 37 71 63 29 88 93 4 52 56 9 94 62 89 55 0 72 15 78 2 81 102 82 46 33 51 86 105 53 6 25 24 11 95 44 65 90 22 35 27 28 38 43 106 101 66 103 76 87 26 54 83 18 75 70 48 59 80 61 69 96 104 10 50 92 67 99 77 98 68 39 34 97 100 60 49 45
42 73 5 85 64 79 84 42 1 58 32 47 17 41 21 8 14 57 40 3 74 91 7 16 12 63 71 13 23 52 36 19 62 4 88 55 31 93 9 56 78 51 20 37 30 0 89 2 46 72 86 15 33 94 53 29 44 105 24 25 102 65 27 28 38 35 66 87 76 22 103 69 6 43 106 101 81 82 67 95 11 26 96 90 104 80 92 39 34 97 77 98 54 83 18 75 70 48 59 61 100 45 49 50 10 68 99 60
43 5 85 32 73 1 42 64 23 14 21 8 58 91 47 17 41 79 57 7 93 84 40 31 3 13 12 37 16 19 74 62 63 71 52 0 4 51 30 9 36 20 56 6 55 88 29 89 94 72 90 78 11 102 2 46 15 92 65 105 81 82 87 76 43 86 38 33 35 96 53 44 80 54 83 75 25 18 70 48 59 24 61 10 27 95 106 101 28 66 103 50 104 69 22 99 98 77 67 97 100 68 60 39 34 26 45 49
44 5 73 85 64 42 84 79 1 32 14 57 8 40 3 58 47 17 21 41 7 16 23 91 13 12 74 36 63 71 31 52 4 37 62 19 55 88 9 93 51 89 15 105 56 0 78 20 2 94 46 102 72 86 30 33 53 103 44 29 24 25 65 81 82 95 106 101 27 35 28 66 87 76 38 22 43 69 39 34 104 67 80 26 97 96 98 100 50 6 90 61 59 70 48 18 54 75 11 77 83 99 49 45 92 68 10 60
45 73 5 85 79 64 42 84 1 32 14 57 8 40 58 3 47 17 21 41 7 63 71 23 13 16 52 74 91 62 12 89 36 37 88 19 31 55 93 0 4 51 20 105 9 56 72 15 2 102 78 46 103 65 30 86 53 33 94 29 43 44 25 24 106 101 80 35 38 27 95 28 81 82 66 87 76 22 104 69 34 39 67 26 90 6 97 99 11 98 92 100 96 50 77 61 49 59 45 48 70 18 68 75 54 83 10 60
46 5 73 32 85 1 42 64 14 23 91 8 7 21 40 58 3 47 17 41 13 57 16 79 31 62 12 84 63 52 71 74 4 0 51 36 78 37 9 38 30 93 19 35 29 55 88 89 56 20 46 2 15 6 72 90 76 87 102 94 11 92 86 66 44 67 53 69 105 27 28 45 65 68 60 81 25 82 33 49 24 43 106 101 95 80 83 54 96 75 104 18 70 48 59 61 22 10 77 50 103 98 97 100 26 99 34 39
47 85 5 73 84 42 79 32 1 64 58 21 14 47 17 57 41 8 74 40 19 23 91 3 16 7 13 12 31 93 63 71 52 94 36 55 20 56 62 37 4 88 30 51 0 9 102 15 46 89 78 29 2 105 72 95 81 82 33 86 53 25 6 44 24 35 38 27 106 101 11 28 65 103 66 87 76 22 90 104 69 26 96 67 43 83 54 18 75 48 70 59 61 92 98 97 50 100 80 10 77 99 39 34 68 45 49 60
48 85 5 73 42 32 64 1 14 8 58 21 47 17 41 57 40 84 3 79 23 91 16 74 7 12 19 31 13 15 36 4 37 88 9 30 56 20 93 89 94 78 33 71 63 106 101 29 2 46 86 55 72 52 105 0 98 53 62 104 102 25 44 24 97 81 82 100 27 65 51 28 95 35 22 66 87 76 38 69 103 43 67 11 26 6 90 80 77 96 83 75 18 54 48 70 59 61 50 10 92 49 68 45 99 39 34 60
49 73 85 5 64 42 1 32 58 79 84 8 14 47 17 41 21 40 57 91 3 74 7 23 13 16 12 19 88 36 31 4 63 71 72 30 52 56 9 37 93 62 2 29 20 78 55 15 0 89 46 51 102 65 105 94 86 33 43 53 6 44 24 25 11 35 80 106 101 81 22 82 27 87 76 90 28 38 95 103 66 104 69 92 26 67 83 54 75 18 70 48 59 61 97 96 10 50 100 98 77 99 68 49 45 39 34 60
import pytest
import pandas as pd
import os
from numpy.testing import assert_array_almost_equal, assert_array_equal
from mlpy import borda_count as borda_mlpy
from .borda import borda_count_np
@pytest.fixture()
def docstring_example():
"""Returns the example reported in MLPY borda_count docstring"""
x = [
[2, 4, 1, 3, 0], # first ranked list
[3, 4, 1, 2, 0], # second ranked list
[2, 4, 3, 0, 1], # third ranked list
[0, 1, 4, 2, 3], # fo`urth ranked list
]
return x
@pytest.fixture()
def ranking_lists_with_reps():
"""Returns the example reported in MLPY borda_count docstring"""
x = [
[2, 4, 3, 3, 0], # first ranked list
[3, 4, 1, 2, 0], # second ranked list
[2, 4, 1, 0, 1], # third ranked list
[0, 1, 2, 2, 3], # fourth ranked list
]
return x
@pytest.fixture()
def data_folder():
return os.path.abspath(os.path.dirname(__file__))
@pytest.fixture()
def dap_rankings(data_folder):
"""Loads a Ranking example as returned by a DAP execution"""
fp = os.path.join(data_folder, "metric_ranking_dap.csv")
df = pd.read_csv(fp, header=0, index_col=0)
return df.values
def _compare_borda_arrays(fixture, top_k=None):
ids_mlpy, count_exts_mlpy, mean_pos_mlpy = borda_mlpy(fixture, k=top_k)
ids_np, count_exts_np, mean_pos_np = borda_count_np(fixture, k=top_k)
assert_array_equal(ids_mlpy, ids_np, "Rankings are Different")
assert_array_equal(
count_exts_mlpy, count_exts_np, "Count Extractions are Different"
)
assert_array_almost_equal(
mean_pos_mlpy, mean_pos_np, decimal=6, err_msg="Mean Positions are Different"
)
def test_dummy_ranking(docstring_example):
_compare_borda_arrays(docstring_example)
def test_borda_numpy_implementation_dap_ranking(dap_rankings):
_compare_borda_arrays(dap_rankings)
# Test and Compare Borda results at different values of TopK selections
def test_dummy_ranking_top_k(docstring_example):
for top_k in (1, 2, 3, 4):
_compare_borda_arrays(docstring_example, top_k=top_k)
def test_dap_ranking_top_k(dap_rankings):
for top_k in (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 107):
_compare_borda_arrays(dap_rankings, top_k=top_k)
def test_exception_is_raised_with_k_out_of_range(docstring_example):
# Testing MLPY implementation first for comparability
with pytest.raises(ValueError):
borda_mlpy(docstring_example, k=0)
with pytest.raises(ValueError):
borda_count_np(docstring_example, k=0)
# Testing MLPY implementation first for comparability
with pytest.raises(ValueError):
borda_mlpy(docstring_example, k=6)
with pytest.raises(ValueError):
borda_count_np(docstring_example, k=6)
def test_value_error_rankings_list_with_repetitions(ranking_lists_with_reps):
with pytest.raises(ValueError):
borda_count_np(ranking_lists_with_reps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment