lenskit.algorithms.basic#

Basic utility algorithms and combiners.

Classes

AllItemsCandidateSelector()

CandidateSelector that selects all items, regardless of whether the user has rated them, as candidates.

EmptyCandidateSelector()

CandidateSelector that never returns any candidates.

Fallback()

The Fallback algorithm predicts with its first component, uses the second to fill in missing values, and so forth.

KnownRating()

The known rating algorithm memorizes ratings provided in the fit method.

Memorized(scores)

The memorized algorithm memorizes socres provided at construction time (not training time).

PopScore([score_method])

Score items by their popularity.

Random([selector, rng_spec])

A random-item recommender.

UnratedItemCandidateSelector()

CandidateSelector that selects items a user has not rated as candidates.

class lenskit.algorithms.basic.PopScore(score_method='quantile')#

Bases: Predictor

Score items by their popularity. Use with TopN to get a most-popular-items recommender.

Parameters:

score_type (str) –

The method for computing popularity scores. Can be one of the following:

  • 'quantile' (the default)

  • 'rank'

  • 'count'

item_pop_#

Item popularity scores.

Type:

pandas.Series

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.

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.basic.Memorized(scores)#

Bases: Predictor

The memorized algorithm memorizes socres provided at construction time (not training time).

Parameters:

scores (DataFrame)

fit(*args, **kwargs)#

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

Parameters:
  • data – 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.

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.basic.Fallback(algorithms: Iterable[Predictor])#
class lenskit.algorithms.basic.Fallback(algorithms: Predictor, *others: Predictor)

Bases: Predictor

The Fallback algorithm predicts with its first component, uses the second to fill in missing values, and so forth.

Parameters:

algorithms (list[Predictor])

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.

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.basic.EmptyCandidateSelector#

Bases: CandidateSelector

CandidateSelector that never returns any candidates.

dtype_#

alias of int64

fit(data, **kwarsg)#

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.

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.

class lenskit.algorithms.basic.UnratedItemCandidateSelector#

Bases: CandidateSelector

CandidateSelector that selects items a user has not rated as candidates. When this selector is fit, it memorizes the rated items.

items_#

All known items.

Type:

lenskit.data.vocab.Vocabulary

users_#

All known users.

Type:

lenskit.data.vocab.Vocabulary

user_items_#

Items rated by each known user, as positions in the items index.

Type:

lenskit.data.matrix.CSRStructure

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.

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.

class lenskit.algorithms.basic.AllItemsCandidateSelector#

Bases: CandidateSelector

CandidateSelector that selects all items, regardless of whether the user has rated them, as candidates. When this selector is fit, it memorizes the set of items.

items_#

All known items.

Type:

numpy.ndarray

fit(ratings, **kwargs)#

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

Parameters:
  • data – 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.

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.

class lenskit.algorithms.basic.Random(selector=None, rng_spec=None)#

Bases: Recommender

A random-item recommender.

selector#

Selects candidate items for recommendation. Default is UnratedItemCandidateSelector.

Type:

CandidateSelector

rng_spec#

Seed or random state for generating recommendations. Pass 'user' to deterministically derive per-user RNGS from the user IDs for reproducibility.

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.

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

class lenskit.algorithms.basic.KnownRating#

Bases: Predictor

The known rating algorithm memorizes ratings provided in the fit method.

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.

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