r_precision_at_k drops failing users from the mean instead of scoring them 0, so the score rises as more users fail
r_precision_at_k in recommenders/evaluation/python_evaluation.py drops users it fails on from the average instead of scoring them 0. A user with no hit in their top R does not pull the score down; they are removed from the mean. As a result, a recommender that fails every user except one still reports a perfect 1.0, and the score rises as coverage gets worse.
recommenders/evaluation/python_evaluation.py:596-613
df_hit, df_hit_count, n_users = merge_ranking_true_pred(
rating_true=rating_true,
rating_pred=rating_pred,
col_user=col_user,
col_item=col_item,
col_prediction=col_prediction,
relevancy_method=relevancy_method,
k=k,
threshold=threshold,
)
if df_hit.shape[0] == 0:
return 0.0
df_merged = df_hit.merge(df_hit_count[[col_user, 'actual']])
df_merged = df_merged[df_merged['rank'] <= df_merged['actual']]
return (df_merged.groupby(col_user).size() / df_hit_count.set_index(col_user)['actual']).mean()The docstring defines R-precision as "the precision@R for each user". merge_ranking_true_pred (lines 379-454) returns n_users, the size of the common-user set (lines 411-414). precision_at_k (line 507) and recall_at_k (line 554) divide by n_users; r_precision_at_k receives it and never uses it. It takes a bare pandas .mean() over an index-aligned division of two Series, and each Series has already lost the users the function failed on:
df_hit_countis built with an inner merge (lines 446-452). A user with no hits anywhere in their top-k list never enters it.- A user who is in
df_hit_count(they have a hit somewhere in the top k) still drops out of the numeratordf_merged.groupby(col_user).size()if every hit is at a rank greater than R. Therank <= actualfilter removes those rows, so the user's key appears only in the denominator.
In both cases the user's term becomes NaN: they are missing from both Series, or pandas aligns by label and fills a key present on only one side of / with NaN. Series.mean() defaults to skipna=True, so the user is left out of the average rather than counted as 0. Failing users shrink the count the mean divides by instead of lowering the score.
Measured
r_precision_at_k, merge_ranking_true_pred and get_top_k_items were copied into a standalone script and run with real pandas 2.3.1, without importing the package. As a calibration check, the copy reproduces the values pinned in test_python_r_precision for the rating_true/rating_pred fixture: 0.21111, 0.24444 and 0.37777 at k=3, 5 and 10.
| Case | r_precision_at_k (as written) |
Expected (precision@R averaged over all common users) |
|---|---|---|
| U1 hits (rank 1 ≤ R=1); U2 has no hit at all (k=2) | 1.0 | 0.5 |
| U1 hits (rank 1 ≤ R=1); U2 hits but at rank 2 > R=1 (k=2) | 1.0 | 0.5 |
| Both users hit within R (control, k=1) | 1.0 | 1.0 (agrees; no user fails) |
| 1 of 5 users hits at rank 1, other 4 miss completely (k=2) | 1.0 | 0.2 (precision_at_k on the same data reports 0.1) |
test_python_r_precision (tests/unit/recommenders/evaluation/test_python_evaluation.py:424-441) does not reach this path:
rating_pred=rating_true: every user hits.rating_nohit: every user misses, so the function returns at thedf_hit.shape[0] == 0guard before the.mean()line.- The shared
rating_predfixture at k=3, 5 and 10: every user (1, 2 and 3) has at least one hit within their own R at each tested k, so no user is dropped.
No fixture has a user who fails while another succeeds, so the partial-failure case is untested.
Consequence
A pipeline that uses r_precision_at_k as a leaderboard or regression-gate metric will see the score go up, not down, as the model fails a growing share of users (cold-start users, out-of-catalog predictions, users for whom the ranker returns nothing relevant). A model that serves 1 of 5 users perfectly and fails the rest reports the same 1.0 as a model that serves all 5 perfectly. The result also disagrees with precision_at_k and recall_at_k, which already count such users as 0 by dividing by n_users.
Suggested fix
Normalize by n_users as the sibling metrics do. Users whose term is NaN (hits only beyond R) then count as 0, and users missing from df_hit_count (no hits) also count as 0:
per_user = df_merged.groupby(col_user).size() / df_hit_count.set_index(col_user)['actual']
return per_user.fillna(0).sum() / n_usersWith this change the four cases above return 0.5, 0.5, 1.0 and 0.2. The three existing rating_pred assertions are unchanged (0.21111, 0.24444, 0.37777), so the current test keeps passing. A regression test with a fixture that mixes users who hit within R, users who hit only beyond R, and users with no hit would cover the path going forward.
#2310 is a precedent for this kind of fix in the same call chain. It fixed merge_ranking_true_pred's by_threshold path, which let precision_at_k, ndcg_at_k, map and map_at_k exceed their documented [0, 1] bound, and added a dedicated regression test (test_python_ranking_metrics_by_threshold). The open Spark R-precision PR #2219 touches only spark_evaluation.py and does not change the Python implementation. Per repo convention, the PR would target staging.
Happy to open the PR.
Source: recommenders-team/recommenders