Skip to content

Commit

Permalink
fix time-bounded popularity
Browse files Browse the repository at this point in the history
  • Loading branch information
mdekstrand committed Jan 21, 2025
1 parent 9c49413 commit 56504c6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
13 changes: 5 additions & 8 deletions lenskit/lenskit/basic/popularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,19 @@ def train(self, data: Dataset, options: TrainingOptions = TrainingOptions()):
return

_log.info("counting time-bounded item popularity")
log = data.interaction_table(format="pandas")
log = data.interaction_table(format="pandas", original_ids=True)

item_scores = None
if "timestamps" not in log.columns:
if "timestamp" not in log.columns:
_log.warning("no timestamps in interaction log; falling back to PopScorer")
super().train(data, options)
return
else:
counts = np.zeros(data.item_count, dtype=np.int32)
start_timestamp = self.config.cutoff.timestamp()
item_nums = log["item_num"][log["timestamp"] > start_timestamp]
np.add.at(counts, item_nums, 1)
item_ids = log["item_id"][log["timestamp"] > start_timestamp]
counts = item_ids.value_counts().reindex(data.items.index, fill_value=0)

item_scores = super()._train_internal(
pd.Series(counts, index=data.items.index),
)
item_scores = super()._train_internal(counts)

self.items_ = data.items.copy()
self.item_scores_ = np.require(item_scores.reindex(self.items_.ids()).values, np.float32)
4 changes: 2 additions & 2 deletions lenskit/tests/basic/test_time_bounded_popular.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
two_days_ago = ts - timedelta(days=2)
simple_df = pd.DataFrame(
{
"item": [1, 2, 2, 3],
"user": [10, 12, 10, 13],
"item_id": [1, 2, 2, 3],
"user_id": [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]],
}
Expand Down

0 comments on commit 56504c6

Please sign in to comment.