-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Recency Aware Collaborative Filtering for Next Basket Recommendat…
…ion (UPCF) Model (#587) * Add Recency Aware Collaborative Filtering for Next Basket Recommendation (UPCF) Model * Remove unused seed variable * Fixed similaripy version
- Loading branch information
Showing
7 changed files
with
204 additions
and
0 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
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,16 @@ | ||
# Copyright 2023 The Cornac Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================ | ||
|
||
from .recom_upcf import UPCF |
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,133 @@ | ||
# Copyright 2023 The Cornac Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================ | ||
|
||
import itertools | ||
|
||
import numpy as np | ||
from scipy.sparse import csr_matrix, vstack | ||
|
||
from ..recommender import NextBasketRecommender | ||
|
||
|
||
class UPCF(NextBasketRecommender): | ||
"""User Popularity-based CF (UPCF) | ||
Parameters | ||
---------- | ||
name: string, default: 'UPCF' | ||
The name of the recommender model. | ||
recency: int, optional, default: 1 | ||
The size of recency window. | ||
If 0, all baskets will be used. | ||
locality: int, optional, default: 1 | ||
The strength we enforce the similarity between two items within a basket | ||
asymmetry: float, optional, default: 0.25 | ||
Trade-off parameter which balances the importance of the probability of having item i given j and probability having item j given i. | ||
This value will be computed via `similaripy.asymetric_cosine`. | ||
verbose: boolean, optional, default: False | ||
When True, running logs are displayed. | ||
References | ||
---------- | ||
Guglielmo Faggioli, Mirko Polato, and Fabio Aiolli. 2020. | ||
Recency Aware Collaborative Filtering for Next Basket Recommendation. | ||
In Proceedings of the 28th ACM Conference on User Modeling, Adaptation and Personalization (UMAP '20). Association for Computing Machinery, New York, NY, USA, 80–87. https://doi.org/10.1145/3340631.3394850 | ||
""" | ||
|
||
def __init__( | ||
self, | ||
name="UPCF", | ||
recency=1, | ||
locality=1, | ||
asymmetry=0.25, | ||
verbose=False, | ||
): | ||
super().__init__(name=name, trainable=False, verbose=verbose) | ||
self.recency = recency | ||
self.locality = locality | ||
self.asymmetry = asymmetry | ||
|
||
def fit(self, train_set, val_set=None): | ||
super().fit(train_set=train_set, val_set=val_set) | ||
self.user_wise_popularity = vstack( | ||
[ | ||
self._get_user_wise_popularity(basket_items) | ||
for _, _, [basket_items] in train_set.ubi_iter( | ||
batch_size=1, shuffle=False | ||
) | ||
] | ||
) | ||
(u_indices, i_indices, r_values) = train_set.uir_tuple | ||
self.user_item_matrix = csr_matrix( | ||
(r_values, (u_indices, i_indices)), | ||
shape=(train_set.num_users, self.total_items), | ||
dtype="float32", | ||
) | ||
return self | ||
|
||
def _get_user_wise_popularity(self, basket_items): | ||
users = [] | ||
items = [] | ||
scores = [] | ||
recent_basket_items = ( | ||
basket_items[-self.recency :] if self.recency > 0 else basket_items | ||
) | ||
for iid in list(set(itertools.chain.from_iterable(recent_basket_items))): | ||
users.append(0) | ||
items.append(iid) | ||
denominator = ( | ||
min(self.recency, len(recent_basket_items)) | ||
if self.recency > 0 | ||
else len(recent_basket_items) | ||
) | ||
numerator = sum([1 for items in recent_basket_items if iid in items]) | ||
scores.append(numerator / denominator) | ||
return csr_matrix( | ||
(scores, (users, items)), shape=(1, self.total_items), dtype="float32" | ||
) | ||
|
||
def score(self, user_idx, history_baskets, **kwargs): | ||
import similaripy as sim | ||
|
||
items = list(set(itertools.chain.from_iterable(history_baskets))) | ||
current_user_item_matrix = csr_matrix( | ||
(np.ones(len(items)), (np.zeros(len(items)), items)), | ||
shape=(1, self.total_items), | ||
dtype="float32", | ||
) | ||
current_user_wise_popularity = self._get_user_wise_popularity(history_baskets) | ||
user_wise_popularity = vstack( | ||
[current_user_wise_popularity, self.user_wise_popularity] | ||
) | ||
user_item_matrix = vstack([current_user_item_matrix, self.user_item_matrix]) | ||
user_sim = sim.asymmetric_cosine( | ||
user_item_matrix, alpha=self.asymmetry, target_rows=[0], verbose=False | ||
) | ||
scores = ( | ||
sim.dot_product( | ||
user_sim.power(self.locality).tocsr()[0], | ||
user_wise_popularity, | ||
verbose=False, | ||
) | ||
.toarray() | ||
.squeeze() | ||
) | ||
|
||
return scores |
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 @@ | ||
similaripy==0.1.3 |
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,50 @@ | ||
# Copyright 2023 The Cornac Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================ | ||
"""Example of Recency Aware Collaborative Filtering for Next Basket Recommendation (UPCF)""" | ||
|
||
import cornac | ||
from cornac.eval_methods import NextBasketEvaluation | ||
from cornac.metrics import NDCG, HitRatio, Recall | ||
from cornac.models import UPCF | ||
|
||
data = cornac.datasets.tafeng.load_basket( | ||
reader=cornac.data.Reader( | ||
min_basket_size=3, max_basket_size=50, min_basket_sequence=2 | ||
) | ||
) | ||
|
||
next_basket_eval = NextBasketEvaluation( | ||
data=data, fmt="UBITJson", test_size=0.2, val_size=0.08, seed=123, verbose=True | ||
) | ||
|
||
models = [ | ||
UPCF( | ||
recency=1, | ||
locality=1, | ||
asymmetry=0.25, | ||
verbose=True, | ||
) | ||
] | ||
|
||
metrics = [ | ||
Recall(k=10), | ||
Recall(k=50), | ||
NDCG(k=10), | ||
NDCG(k=50), | ||
HitRatio(k=10), | ||
HitRatio(k=50), | ||
] | ||
|
||
cornac.Experiment(eval_method=next_basket_eval, models=models, metrics=metrics).run() |