-
Notifications
You must be signed in to change notification settings - Fork 58
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 #270 from mdekstrand/feature/split-hpf
Split HPF into a separate package
- Loading branch information
Showing
8 changed files
with
56 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Add-On Algorithms | ||
================= | ||
|
||
Several add-on packages to LensKit provide additional collections of algorithms or bridges | ||
to implementations in other packages. | ||
|
||
.. toctree:: | ||
:caption: External Algorithms | ||
|
||
Poisson factorization <https://lkpy.lenskit.org/projects/lenskit-hpf/> |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,56 +1 @@ | ||
import logging | ||
|
||
import pandas as pd | ||
|
||
from .mf_common import MFPredictor | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class HPF(MFPredictor): | ||
""" | ||
Hierarchical Poisson factorization, provided by | ||
`hpfrec <https://hpfrec.readthedocs.io/en/latest/>`_. | ||
Args: | ||
features(int): the number of features | ||
**kwargs: arguments passed to :py:class:`hpfrec.HPF`. | ||
""" | ||
|
||
def __init__(self, features, **kwargs): | ||
self.features = features | ||
self._kwargs = kwargs | ||
|
||
def fit(self, ratings, **kwargs): | ||
import hpfrec | ||
|
||
users = pd.Index(ratings.user.unique()) | ||
items = pd.Index(ratings.item.unique()) | ||
|
||
if 'rating' in ratings.columns: | ||
count = ratings.rating.values.copy() | ||
else: | ||
_logger.info('no ratings found, assuming 1.0') | ||
count = 1.0 | ||
|
||
hpfdf = pd.DataFrame({ | ||
'UserId': users.get_indexer(ratings.user), | ||
'ItemId': items.get_indexer(ratings.item), | ||
'Count': count | ||
}) | ||
|
||
hpf = hpfrec.HPF(self.features, reindex=False, **self._kwargs) | ||
|
||
_logger.info('fitting HPF model with %d features', self.features) | ||
hpf.fit(hpfdf) | ||
|
||
self.user_index_ = users | ||
self.item_index_ = items | ||
self.user_features_ = hpf.Theta | ||
self.item_features_ = hpf.Beta | ||
|
||
return self | ||
|
||
def predict_for_user(self, user, items, ratings=None): | ||
# look up user index | ||
return self.score_by_ids(user, items) | ||
from lenskit_hpf import HPF # noqa: F401 |
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 was deleted.
Oops, something went wrong.