-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[FEATURE] Implement R-Precision with PySpark #2087 #2219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
demoncoder-crypto
wants to merge
3
commits into
recommenders-team:staging
Choose a base branch
from
demoncoder-crypto:feature/spark-r-precision
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -328,7 +328,7 @@ | |
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"\r", | ||
"\r\n", | ||
"[Stage 0:> (0 + 1) / 1]\r" | ||
] | ||
}, | ||
|
@@ -350,7 +350,7 @@ | |
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"\r", | ||
"\r\n", | ||
" \r" | ||
] | ||
} | ||
|
@@ -773,7 +773,34 @@ | |
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 2.2.8 ROC and AUC\n", | ||
"#### 2.2.8 R-Precision\n", | ||
"\n", | ||
"R-Precision evaluates the fraction of relevant items among the top R recommended items, where R is the total number of *truly* relevant items for a specific user. It's equivalent to Recall@R.\n", | ||
"\n", | ||
"**Difference from Precision@k:** Precision@k measures relevance within a fixed top *k* items, regardless of the total number of relevant items (R). R-Precision adapts the evaluation depth (*R*) based on the user's specific ground truth, making it potentially more user-centric when the number of relevant items varies significantly across users.\n", | ||
Comment on lines
+776
to
+780
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get an error when trying to compute the notebook:
|
||
"\n", | ||
"**Difference from Recall@k:** Recall@k measures how many of the *total* relevant items (R) are found within the top *k* recommendations. R-Precision focuses specifically on the precision within the top *R* items." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Note: The spark_rank_eval object was initialized with k=3. \n", | ||
"# R-Precision intrinsically uses R (number of relevant items for the user) as the cutoff.\n", | ||
"# The 'k' parameter passed during initialization doesn't directly affect R-Precision calculation itself,\n", | ||
"# but it might affect how the rating_pred dataframe is pre-processed if relevancy_method relies on k.\n", | ||
"# For a direct comparison with other metrics at a fixed k, ensure the underlying data processing is consistent.\n", | ||
"print(f\"The R-Precision is {spark_rank_eval.r_precision()}\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"#### 2.2.9 ROC and AUC\n", | ||
"\n", | ||
"ROC, as well as AUC, is a well known metric that is used for evaluating binary classification problem. It is similar in the case of binary rating typed recommendation algorithm where the \"hit\" accuracy on the relevant items is used for measuring the recommender's performance. \n", | ||
"\n", | ||
|
@@ -1972,4 +1999,4 @@ | |
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -515,3 +515,58 @@ def test_serendipity_item_feature_vector(spark_diversity_data): | |
col_relevance="Relevance", | ||
) | ||
assert evaluator.serendipity() == pytest.approx(0.4028, TOL) | ||
|
||
|
||
@pytest.mark.spark | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same with the tests:
|
||
def test_spark_r_precision(spark_data): | ||
df_true, df_pred = spark_data | ||
|
||
# Test perfect prediction (R-Precision should be 1.0) | ||
evaluator_perfect = SparkRankingEvaluation(df_true, df_true, col_prediction="rating") | ||
assert evaluator_perfect.r_precision() == pytest.approx(1.0, TOL) | ||
|
||
# Test with sample prediction data | ||
evaluator = SparkRankingEvaluation(df_true, df_pred) | ||
# Expected value calculation: | ||
# User 1: R=3 relevant items (1, 2, 3). Top 3 predictions: (1, 0.8), (5, 0.6), (2, 0.4). Relevant in top 3: (1, 2). R-Prec = 2/3 | ||
# User 2: R=2 relevant items (1, 4). Top 2 predictions: (1, 0.9), (4, 0.7). Relevant in top 2: (1, 4). R-Prec = 2/2 = 1.0 | ||
# User 3: R=1 relevant item (2). Top 1 prediction: (2, 0.7). Relevant in top 1: (2). R-Prec = 1/1 = 1.0 | ||
# Mean R-Precision = (2/3 + 1.0 + 1.0) / 3 = (0.6666... + 1 + 1) / 3 = 2.6666... / 3 = 0.8888... | ||
expected_r_precision = (2/3 + 1.0 + 1.0) / 3 | ||
assert evaluator.r_precision() == pytest.approx(expected_r_precision, TOL) | ||
|
||
# Test with different k values | ||
# Using k=3 | ||
evaluator_k3 = SparkRankingEvaluation(df_true, df_pred, k=3) | ||
# When k=3, we're still getting the same top R predictions for each user since all users have R ≤ 3 | ||
assert evaluator_k3.r_precision() == pytest.approx(expected_r_precision, TOL) | ||
|
||
# Using k=5 | ||
evaluator_k5 = SparkRankingEvaluation(df_true, df_pred, k=5) | ||
# When k=5, we're still getting the same top R predictions for each user | ||
assert evaluator_k5.r_precision() == pytest.approx(expected_r_precision, TOL) | ||
|
||
# Test that r_precision is equivalent to precision when R is the same for all users | ||
# and equal to k (comparing to precision_at_k test) | ||
# We limit the data to users with the same number of relevant items | ||
# and set k to that number | ||
same_r_df_true = df_true.filter("userID = 1") # User 1 has R=3 | ||
k_value = 3 # Same as R for user 1 | ||
r_precision_evaluator = SparkRankingEvaluation(same_r_df_true, df_pred, k=k_value) | ||
precision_evaluator = SparkRankingEvaluation(same_r_df_true, df_pred, k=k_value) | ||
assert r_precision_evaluator.r_precision() == pytest.approx(precision_evaluator.precision_at_k(), TOL) | ||
|
||
# Test case where a user has no relevant items (ensure they are ignored) | ||
# Add a user 4 with only predictions, no ground truth | ||
spark = df_pred.sql_ctx.sparkSession | ||
new_pred_row = spark.createDataFrame([(4, 1, 0.9), (4, 2, 0.8)], df_pred.columns) | ||
df_pred_extra_user = df_pred.union(new_pred_row) | ||
evaluator_extra = SparkRankingEvaluation(df_true, df_pred_extra_user) | ||
# Result should be the same as before, ignoring user 4 | ||
assert evaluator_extra.r_precision() == pytest.approx(expected_r_precision, TOL) | ||
|
||
# Test case where NO users have relevant items (R=0 for all) | ||
empty_true = df_true.filter("userID > 10") # Create empty ground truth | ||
with pytest.warns(UserWarning, match="No users with relevant items found"): # Check for warning | ||
evaluator_no_relevant = SparkRankingEvaluation(empty_true, df_pred) | ||
assert evaluator_no_relevant.r_precision() == 0.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you submit the fixed version, could you please run the whole notebook so people can see the results?
Please use python 3.11 which is the latest one we support.