-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #493 from ZiyaoWei/main
feat: Add TimeBoundedPopScore for time-bounded popularity
- Loading branch information
Showing
2 changed files
with
143 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# This file is part of LensKit. | ||
# Copyright (C) 2018-2023 Boise State University | ||
# Copyright (C) 2023-2024 Drexel University | ||
# Licensed under the MIT license, see LICENSE.md for details. | ||
# SPDX-License-Identifier: MIT | ||
|
||
import pickle | ||
from datetime import datetime, timedelta | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
from lenskit.basic import popularity | ||
from lenskit.data import from_interactions_df | ||
from lenskit.data.items import ItemList | ||
|
||
ts = datetime(year=2024, month=1, day=1) | ||
one_day_ago = ts - timedelta(days=1) | ||
two_days_ago = ts - timedelta(days=2) | ||
simple_df = pd.DataFrame( | ||
{ | ||
"item": [1, 2, 2, 3], | ||
"user": [10, 12, 10, 13], | ||
"rating": [4.0, 3.0, 5.0, 2.0], | ||
"timestamp": [i.timestamp() for i in [ts, one_day_ago, one_day_ago, one_day_ago]], | ||
} | ||
) | ||
simple_ds = from_interactions_df(simple_df) | ||
|
||
|
||
def test_time_bounded_pop_score_quantile_one_day_window(): | ||
algo = popularity.TimeBoundedPopScore(one_day_ago) | ||
algo.train(simple_ds) | ||
assert algo.item_scores_.equals(pd.Series([1.0, 0.0, 0.0], index=[1, 2, 3])) | ||
|
||
|
||
def test_time_bounded_pop_score_quantile_one_day_window_call_interface(): | ||
algo = popularity.TimeBoundedPopScore(one_day_ago) | ||
algo.train(simple_ds) | ||
p = algo(ItemList(item_ids=[1, 2, 3])) | ||
|
||
assert len(p) == 3 | ||
assert (p.scores() == [1.0, 0.0, 0.0]).all() | ||
|
||
|
||
def test_time_bounded_pop_score_quantile_two_day_window(): | ||
algo = popularity.TimeBoundedPopScore(two_days_ago) | ||
algo.train(simple_ds) | ||
assert algo.item_scores_.equals(pd.Series([0.25, 1.0, 0.5], index=[1, 2, 3])) | ||
|
||
|
||
def test_time_bounded_pop_score_fallbacks_to_pop_score_for_dataset_without_timestamps(): | ||
ds = from_interactions_df(simple_df.drop(columns=["timestamp"])) | ||
|
||
algo = popularity.TimeBoundedPopScore(one_day_ago) | ||
algo.train(ds) | ||
assert algo.item_scores_.equals(pd.Series([0.25, 1.0, 0.5], index=[1, 2, 3])) | ||
|
||
|
||
def test_time_bounded_pop_score_rank(): | ||
algo = popularity.TimeBoundedPopScore(two_days_ago, "rank") | ||
algo.train(simple_ds) | ||
assert algo.item_scores_.equals(pd.Series([1.5, 3.0, 1.5], index=[1, 2, 3])) | ||
|
||
|
||
def test_time_bounded_pop_score_counts(): | ||
algo = popularity.TimeBoundedPopScore(two_days_ago, "count") | ||
algo.train(simple_ds) | ||
assert algo.item_scores_.equals(pd.Series([1, 2, 1], index=[1, 2, 3], dtype=np.int32)) | ||
|
||
|
||
def test_time_bounded_pop_score_save_load(): | ||
original = popularity.TimeBoundedPopScore(one_day_ago) | ||
original.train(simple_ds) | ||
|
||
mod = pickle.dumps(original) | ||
algo = pickle.loads(mod) | ||
|
||
assert all(algo.item_scores_ == original.item_scores_) |