Skip to content
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 3 commits into from
Jul 15, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions reco_utils/evaluation/python_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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"]
]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand All @@ -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"`
Copy link
Collaborator

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

Copy link
Collaborator

@gramhagen gramhagen Jul 12, 2019

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 =)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

"""

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can avoid the repeated groupby too

groups = dataframe.groupby(col_user, as_index=False)
top_k_items = groups.apply(lambda x: x.nlargest(k, col_rating)).reset_index(drop=True)
top_k_items["rank"] = groups.cumcount() + 1

return top_k_items


"""Function name and function mapper.
Expand Down