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

PCRL handles unknown users and/or items #451

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
32 changes: 29 additions & 3 deletions cornac/models/pcrl/recom_pcrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import numpy as np
from ..recommender import Recommender
from ...utils.common import scale
from ...exception import ScoreException


class PCRL(Recommender):
Expand Down Expand Up @@ -191,10 +193,34 @@ def score(self, user_idx, item_idx=None):
"""

if item_idx is None:
user_pred = self.Beta * self.Theta[user_idx, :].T
if self.train_set.is_unk_user(user_idx):
raise ScoreException(
"Can't make score prediction for (user_id=%d)" % user_idx
)

user_pred = self.Beta.dot(self.Theta[user_idx, :].T)
else:
if self.train_set.is_unk_user(user_idx) or self.train_set.is_unk_item(
item_idx
):
raise ScoreException(
"Can't make score prediction for (user_id=%d, item_id=%d)"
% (user_idx, item_idx)
)

user_pred = self.Beta[item_idx, :] * self.Theta[user_idx, :].T
# transform user_pred to a flatten array
if self.train_set.min_rating == self.train_set.max_rating:
user_pred = scale(user_pred, 0.0, self.train_set.max_rating, 0.0, 1.0)
else:
user_pred = scale(
user_pred,
self.train_set.min_rating,
self.train_set.max_rating,
0.0,
1.0,
)

# transform user_pred to a flatten array
user_pred = np.array(user_pred, dtype="float64").flatten()

return user_pred