-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Python evaluator module fix #863
Merged
Merged
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 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 |
---|---|---|
|
@@ -363,7 +363,7 @@ def merge_ranking_true_pred( | |
|
||
Returns: | ||
pd.DataFrame, pd.DataFrame, int: | ||
DataFrame of recommendation hits | ||
DataFrame of recommendation hits, sorted by `col_user` and `"rank"` | ||
DataFrmae of hit counts vs actual relevant items per user | ||
number of unique user ids | ||
""" | ||
|
@@ -390,9 +390,6 @@ def merge_ranking_true_pred( | |
col_rating=col_prediction, | ||
k=top_k, | ||
) | ||
df_hit["rank"] = df_hit.groupby(col_user)[col_prediction].rank( | ||
method="first", ascending=False | ||
) | ||
df_hit = pd.merge(df_hit, rating_true_common, on=[col_user, col_item])[ | ||
[col_user, col_item, "rank"] | ||
] | ||
|
@@ -559,7 +556,7 @@ def ndcg_at_k( | |
# relevance in this case is always 1 | ||
df_dcg["dcg"] = 1 / np.log1p(df_dcg["rank"]) | ||
# sum up discount gained to get discount cumulative gain | ||
df_dcg = df_dcg.groupby(col_user, as_index=False).agg({"dcg": "sum"}) | ||
df_dcg = df_dcg.groupby(col_user, as_index=False, sort=False).agg({"dcg": "sum"}) | ||
# calculate ideal discounted cumulative gain | ||
df_ndcg = pd.merge(df_dcg, df_hit_count, on=[col_user]) | ||
df_ndcg["idcg"] = df_ndcg["actual"].apply( | ||
|
@@ -625,8 +622,8 @@ def map_at_k( | |
return 0.0 | ||
|
||
# calculate reciprocal rank of items for each user and sum them up | ||
df_hit_sorted = df_hit.sort_values([col_user, "rank"]) | ||
df_hit_sorted["rr"] = (df_hit.groupby(col_user).cumcount() + 1) / df_hit["rank"] | ||
df_hit_sorted = df_hit.copy() | ||
df_hit_sorted["rr"] = (df_hit_sorted.groupby(col_user).cumcount() + 1) / df_hit_sorted["rank"] | ||
df_hit_sorted = df_hit_sorted.groupby(col_user).agg({"rr": "sum"}).reset_index() | ||
|
||
df_merge = pd.merge(df_hit_sorted, df_hit_count, on=col_user) | ||
|
@@ -651,14 +648,16 @@ def get_top_k_items( | |
k (int): number of items for each user | ||
|
||
Returns: | ||
pd.DataFrame: DataFrame of top k items for each user | ||
pd.DataFrame: DataFrame of top k items for each user, sorted by `col_user` and `"rank"` | ||
""" | ||
|
||
return ( | ||
top_k_items = ( | ||
dataframe.groupby(col_user, as_index=False) | ||
.apply(lambda x: x.nlargest(k, col_rating)) | ||
.reset_index(drop=True) | ||
) | ||
top_k_items["rank"] = top_k_items.groupby(col_user).cumcount() + 1 | ||
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. you can avoid the repeated groupby too
|
||
return top_k_items | ||
|
||
|
||
"""Function name and function mapper. | ||
|
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.
i would remove the double quotes from rank to match just the backticks like col_user
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.
also, in the returns section of get_top_k_items =)
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.
good catch!