import numpy as np def apk(actual, predicted, k=10): """ Computes the average precision at k. This function computes the average prescision at k between two lists of items. Parameters ---------- actual : list A list of elements that are to be predicted (order doesn't matter) predicted : list A list of predicted elements (order does matter) k : int, optional The maximum number of predicted elements Returns ------- score : double The average precision at k over the input lists """ if not actual: return 0.0 if len(predicted)>k: predicted = predicted[:k] score = 0.0 num_hits = 0.0 for i,p in enumerate(predicted): # first condition checks whether it is valid prediction # second condition checks if prediction is not repeated if p in actual and p not in predicted[:i]: num_hits += 1.0 score += num_hits / (i+1.0) return score / min(len(actual), k) def mapk(actual: list[list], predicted: list[list], k:int=10) -> float: """ Computes the mean average precision at k. This function computes the mean average prescision at k between two lists of lists of items. Parameters ---------- actual : list A list of lists of elements that are to be predicted (order doesn't matter in the lists) predicted : list A list of lists of predicted elements (order matters in the lists) k : int, optional The maximum number of predicted elements Returns ------- score : double The mean average precision at k over the input lists """ return np.mean([apk(a,p,k) for a,p in zip(actual, predicted)]).astype(float) def rank_biased_overlap(l1,l2,p): """ Returns RBO indefinite rank similarity metric, as described in: Webber, W., Moffat, A., & Zobel, J. (2010). A similarity measure for indefinite rankings. ACM Transactions on Information Systems. doi:10.1145/1852102.1852106. """ sl,ll = sorted([(len(l1), l1),(len(l2),l2)]) s, S = sl l, L = ll # Calculate the overlaps at ranks 1 through l # (the longer of the two lists) ss = set([]) ls = set([]) overs = {} for i in range(l): ls.add(L[i]) if i