-
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 1 commit
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
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,37 @@ 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 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.
self.rating_pred_raw
is only used in the R-Precision. Either use directlyself.rating_pred
or do the data treatment internally in the function