kNN Collaborative Filtering¶
LKPY provides user and itembased classical kNN collaborative Filtering implementations. These lightlyconfigurable implementations are intended to capture the behavior of the Javabased LensKit implementations to provide a good upgrade path and enable basic experiments out of the box.
Itembased kNN¶

class
lenskit.algorithms.item_knn.
ItemItem
(nnbrs, min_nbrs=1, min_sim=1e06, save_nbrs=None, center=True, aggregate='weightedaverage')¶ Bases:
lenskit.algorithms.Trainable
,lenskit.algorithms.Predictor
Itemitem nearestneighbor collaborative filtering with ratings. This itemitem implementation is not terribly configurable; it hardcodes design decisions found to work well in the previous Javabased LensKit code.

load_model
(path)¶ Save a trained model to a file.
Parameters: path (str) – the path to file from which to load the model. Returns: the reloaded model (of an implementationdefined type).

predict
(model, user, items, ratings=None)¶ Compute predictions for a user and items.
Parameters:  model – the trained model to use. Either
None
or the ratings matrix if the algorithm has no concept of training.  user – the user ID
 items (arraylike) – 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:  model – the trained model to use. Either

save_model
(model, path)¶ Save a trained model to a file or directory. The default implementation pickles the model.
Algorithms are allowed to use any format for saving their models, including directories.
Parameters:  model – the trained model.
 path (str) – the path at which to save the model.

train
(ratings)¶ Train a model.
The modeltraining process depends on
save_nbrs
andmin_sim
, but not on other algorithm parameters.Parameters: ratings (pandas.DataFrame) – (user,item,rating) data for computing item similarities. Returns: a trained itemitem CF model.


class
lenskit.algorithms.item_knn.
IIModel
¶ Itemitem recommendation model. This stores the necessary data to run the itembased kNN recommender.

items
¶ the index of item IDs.
Type: pandas.Index

means
¶ the mean rating for each known item.
Type: numpy.ndarray

counts
¶ the number of saved neighbors for each item.
Type: numpy.ndarray

sim_matrix
¶ the similarity matrix.
Type: matrix.CSR

users
¶ the index of known user IDs for the rating matrix.
Type: pandas.Index

rating_matrix
¶ the useritem rating matrix for looking up users’ ratings.
Type: matrix.CSR

Userbased kNN¶

class
lenskit.algorithms.user_knn.
UserUser
(nnbrs, min_nbrs=1, min_sim=0, center=True, aggregate='weightedaverage')¶ Bases:
lenskit.algorithms.Trainable
,lenskit.algorithms.Predictor
Useruser nearestneighbor collaborative filtering with ratings. This useruser implementation is not terribly configurable; it hardcodes design decisions found to work well in the previous Javabased LensKit code.

load_model
(path)¶ Save a trained model to a file.
Parameters: path (str) – the path to file from which to load the model. Returns: the reloaded model (of an implementationdefined type).

predict
(model, user, items, ratings=None)¶ Compute predictions for a user and items.
Parameters:  model (UUModel) – the memorized data to use.
 user – the user ID
 items (arraylike) – 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:

save_model
(model, path)¶ Save a trained model to a file or directory. The default implementation pickles the model.
Algorithms are allowed to use any format for saving their models, including directories.
Parameters:  model – the trained model.
 path (str) – the path at which to save the model.

train
(ratings)¶ “Train” a useruser 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 userbased CF computation. Return type: UUModel


class
lenskit.algorithms.user_knn.
UUModel
¶ Memorized data for useruser collaborative filtering.

matrix
¶ normalized useritem rating matrix.
Type: matrix.CSR

users
¶ index of user IDs.
Type: pandas.Index

user_means
¶ user mean ratings.
Type: numpy.ndarray

items
¶ index of item IDs.
Type: pandas.Index

transpose
¶ the transposed rating matrix (with data transformations but without L2 normalization).
Type: matrix.CSR
