Skip to content

Commit

Permalink
implicit logging and importability
Browse files Browse the repository at this point in the history
  • Loading branch information
mdekstrand committed Nov 28, 2018
1 parent ccd8e29 commit 72dede0
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lenskit/algorithms/implicit.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from collections import namedtuple
import warnings
import logging
import pandas as pd
import numpy as np

from implicit.als import AlternatingLeastSquares
from implicit.bpr import BayesianPersonalizedRanking

from ..matrix import sparse_ratings
from . import Trainable, Recommender

_logger = logging.getLogger(__name__)

ImplicitModel = namedtuple('ImplicitModel', [
'algo', 'matrix', 'users', 'items'
])
Expand Down Expand Up @@ -35,6 +36,8 @@ def train(self, ratings):
matrix, users, items = sparse_ratings(ratings, scipy=True)
iur = matrix.T.tocsr()

_logger.info('training %s on %s matrix (%d nnz)', self.algo_class, iur.shape, iur.nnz)

algo = self.algo_class(*self.algo_args, **self.algo_kwargs)
algo.fit(iur)

Expand Down Expand Up @@ -65,6 +68,7 @@ def __getattr__(self, name):
def __str__(self):
return 'Implicit({}, {}, {})'.format(self.algo_class.__name__, self.algo_args, self.algo_kwargs)


class ALS(BaseRec):
"""
LensKit interface to :py:mod:`implicit.als`.
Expand All @@ -74,6 +78,7 @@ def __init__(self, *args, **kwargs):
Construct an ALS recommender. The arguments are passed as-is to
:py:class:`implicit.als.AlternatingLeastSquares`.
"""
from implicit.als import AlternatingLeastSquares
super().__init__(AlternatingLeastSquares, *args, **kwargs)


Expand All @@ -86,4 +91,5 @@ def __init__(self, *args, **kwargs):
Construct an ALS recommender. The arguments are passed as-is to
:py:class:`implicit.als.BayesianPersonalizedRanking`.
"""
from implicit.bpr import BayesianPersonalizedRanking
super().__init__(BayesianPersonalizedRanking, *args, **kwargs)

0 comments on commit 72dede0

Please sign in to comment.