Spaces:
Running
Running
| 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<s: | |
| ss.add(S[i]) | |
| X_d = len(ss.intersection(ls)) | |
| d = i+1 | |
| overs[d] = float(X_d) | |
| # (1) \sum_{d=1}^l (X_d / d) * p^d | |
| sum1 = 0 | |
| for i in range(l): | |
| d=i+1 | |
| sum1+=overs[d]/d*pow(p,d) | |
| X_s = overs[s] | |
| X_l = overs[l] | |
| # (2) \sum_{d=s+1}^l [(X_s (d - s)) / (sd)] * p^d | |
| sum2 = 0 | |
| for i in range(s,l): | |
| d=i+1 | |
| sum2+=(X_s*(d-s)/(s*d))*pow(p,d) | |
| # (3) [(X_l - X_s) / l + X_s / s] * p^l | |
| sum3 = ((X_l-X_s)/l+X_s/s)*pow(p,l) | |
| # Equation 32. | |
| rbo_ext = (1-p)/p*(sum1+sum2)+sum3 | |
| return rbo_ext | |