lenskit.algorithms#

LensKit algorithms.

The lenskit.algorithms package contains several example algorithms for carrying out recommender experiments. These algorithm implementations are designed to mimic the characteristics of the implementations provided by the original LensKit Java package. It also provides abstract base classes (abc) representing different algorithm capabilities.

Classes

Algorithm()

Base class for LensKit algorithms.

CandidateSelector()

Select candidates for recommendation for a user, possibly with some additional ratings.

Predictor()

Predicts user ratings of items.

Recommender()

Recommends lists of items for users.

class lenskit.algorithms.Algorithm#

Bases: object

Base class for LensKit algorithms. These algorithms follow the SciKit design pattern for estimators.

Canonical:

lenskit.Algorithm

IGNORED_PARAMS = []#

Names of parameters to ignore in get_params().

EXTRA_PARAMS = []#

Names of extra parameters to include in get_params(). Useful when the constructor takes **kwargs.

abstract fit(data, **kwargs)#

Train a model using the specified ratings (or similar) data.

Parameters:
  • data (Dataset) – The training data.

  • kwargs – Additional training data the algorithm may require. Algorithms should avoid using the same keyword arguments for different purposes, so that they can be more easily hybridized.

Returns:

The algorithm object.

Return type:

Self

get_params(deep=True)#

Get the parameters for this algorithm (as in scikit-learn). Algorithm parameters should match constructor argument names.

The default implementation returns all attributes that match a constructor parameter name. It should be compatible with sklearn.base.BaseEstimator.get_params() method so that LensKit alogrithms can be cloned with sklearn.base.clone() as well as lenskit.util.clone().

Returns:

the algorithm parameters.

Return type:

dict

class lenskit.algorithms.Recommender#

Bases: Algorithm

Recommends lists of items for users.

abstract recommend(user, n=None, candidates=None, ratings=None)#

Compute recommendations for a user.

Parameters:
  • user – the user ID

  • n (int) – the number of recommendations to produce (None for unlimited)

  • candidates (array-like) – The set of valid candidate items; if None, a default set will be used. For many algorithms, this is their CandidateSelector.

  • ratings (pandas.Series) – the user’s ratings (indexed by item id); if provided, they may be used to override or augment the model’s notion of a user’s preferences.

Returns:

a frame with an item column; if the recommender also produces scores, they will be in a score column.

Return type:

pandas.DataFrame

classmethod adapt(algo)#

Ensure that an algorithm is a Recommender. If it is not a recommender, it is wrapped in a lenskit.basic.TopN with a default candidate selector.

Note

Since 0.6.0, since algorithms are fit directly, you should call this method before calling Algorithm.fit(), unless you will always be passing explicit candidate sets to recommend().

Parameters:

algo (Predictor) – the underlying rating predictor.

class lenskit.algorithms.Predictor#

Bases: Algorithm

Predicts user ratings of items. Predictions are really estimates of the user’s like or dislike, and the Predictor interface makes no guarantees about their scale or granularity.

Canonical:

lenskit.Predictor

predict(pairs, ratings=None)#

Compute predictions for user-item pairs. This method is designed to be compatible with the general SciKit paradigm; applications typically want to use predict_for_user().

Parameters:
Returns:

The predicted scores for each user-item pair.

Return type:

pandas.Series

abstract predict_for_user(user, items, ratings=None)#

Compute predictions for a user and items.

Parameters:
  • user – the user ID

  • items (array-like) – the items to predict

  • ratings (pandas.Series) – the user’s ratings (indexed by item id); if provided, they may be used to override or augment the model’s notion of a user’s preferences.

Returns:

scores for the items, indexed by item id.

Return type:

pandas.Series

class lenskit.algorithms.CandidateSelector#

Bases: Algorithm

Select candidates for recommendation for a user, possibly with some additional ratings.

UnratedItemCandidateSelector is the default and most common implementation of this interface.

abstract candidates(user, ratings=None)#

Select candidates for the user.

Parameters:
  • user – The user key or ID.

  • ratings (pandas.Series or array-like) – Ratings or items to use instead of whatever ratings were memorized for this user. If a pandas.Series, the series index is used; if it is another array-like it is assumed to be an array of items.

static rated_items(ratings)#

Utility function for converting a series or array into an array of item IDs. Useful in implementations of candidates().

Modules

als

LensKit ALS implementations.

basic

Basic utility algorithms and combiners.

bias

knn

k-NN recommender models.

mf_common

Common utilities & implementations for matrix factorization.

ranking

Algorithms to rank items based on scores.

svd