(rows: List[Dict[str, Any]], gain_fn)
| 84 | |
| 85 | |
| 86 | def dcg(rows: List[Dict[str, Any]], gain_fn) -> float: |
| 87 | score = 0.0 |
| 88 | for idx, row in enumerate(rows, start=1): |
| 89 | gain = float(gain_fn(row)) |
| 90 | if gain <= 0: |
| 91 | continue |
| 92 | score += gain / math.log2(idx + 1) |
| 93 | return score |
| 94 | |
| 95 | |
| 96 | def reciprocal_rank(rows: List[Dict[str, Any]], gain_fn, k: int) -> float: |