k-NN Collaborative Filtering

LKPY provides user- and item-based classical k-NN collaborative Filtering implementations. These lightly-configurable implementations are intended to capture the behavior of the Java-based LensKit implementations to provide a good upgrade path and enable basic experiments out of the box.

Item-based k-NN

class lenskit.algorithms.item_knn.ItemItem(nnbrs, min_nbrs=1, min_sim=1e-06, save_nbrs=None, center=True, aggregate='weighted-average')

Bases: lenskit.algorithms.Predictor

Item-item nearest-neighbor collaborative filtering with ratings. This item-item implementation is not terribly configurable; it hard-codes design decisions found to work well in the previous Java-based LensKit code.

Parameters
  • nnbrs (int) – the maximum number of neighbors for scoring each item (None for unlimited)

  • min_nbrs (int) – the minimum number of neighbors for scoring each item

  • min_sim (double) – minimum similarity threshold for considering a neighbor

  • save_nbrs (double) – the number of neighbors to save per item in the trained model (None for unlimited)

  • center (bool) – whether to normalize (mean-center) rating vectors. Turn this off when working with unary data and other data types that don’t respond well to centering.

  • aggregate – the type of aggregation to do. Can be weighted-average or sum.

item_index_: pandas.Index

the index of item IDs.

item_means_: numpy.ndarray

the mean rating for each known item.

item_counts_: numpy.ndarray

the number of saved neighbors for each item.

sim_matrix_: matrix.CSR

the similarity matrix.

user_index_: pandas.Index

the index of known user IDs for the rating matrix.

rating_matrix_: matrix.CSR

the user-item rating matrix for looking up users’ ratings.

fit(ratings, **kwargs)

Train a model.

The model-training process depends on save_nbrs and min_sim, but not on other algorithm parameters.

Parameters

ratings (pandas.DataFrame) – (user,item,rating) data for computing item similarities.

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

User-based k-NN

class lenskit.algorithms.user_knn.UserUser(nnbrs, min_nbrs=1, min_sim=0, center=True, aggregate='weighted-average')

Bases: lenskit.algorithms.Predictor

User-user nearest-neighbor collaborative filtering with ratings. This user-user implementation is not terribly configurable; it hard-codes design decisions found to work well in the previous Java-based LensKit code.

Parameters
  • nnbrs (int) – the maximum number of neighbors for scoring each item (None for unlimited)

  • min_nbrs (int) – the minimum number of neighbors for scoring each item

  • min_sim (double) – minimum similarity threshold for considering a neighbor

  • center (bool) – whether to normalize (mean-center) rating vectors. Turn this off when working with unary data and other data types that don’t respond well to centering.

  • aggregate – the type of aggregation to do. Can be weighted-average or sum.

user_index_: pandas.Index

User index.

item_index_: pandas.Index

Item index.

user_means_: numpy.ndarray

User mean ratings.

rating_matrix_: matrix.CSR

Normalized user-item rating matrix.

transpose_matrix_: matrix.CSR

Transposed un-normalized rating matrix.

fit(ratings, **kwargs)

“Train” a user-user CF model. This memorizes the rating data in a format that is usable for future computations.

Parameters

ratings (pandas.DataFrame) – (user, item, rating) data for collaborative filtering.

Returns

a memorized model for efficient user-based CF computation.

Return type

UUModel

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, will be used to recompute the user’s bias at prediction time.

Returns

scores for the items, indexed by item id.

Return type

pandas.Series