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.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.

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

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

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

sim_matrix_
¶ the similarity matrix.
Type: matrix.CSR

user_index_
¶ 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

fit
(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.

load
(path)¶ Load a fit algorithm from a file. The default implementation unpickles the object and transplants its parameters and model into this object.
Parameters: file (pathlike) – the file to load.

predict_for_user
(user, items, ratings=None)¶ Compute predictions for a user and items.
Parameters:  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:

save
(path)¶ Save a fit algorithm to a file. The default implementation pickles the object.
Parameters: file (pathlike) – the file to save.

Userbased kNN¶

class
lenskit.algorithms.user_knn.
UserUser
(nnbrs, min_nbrs=1, min_sim=0, center=True, aggregate='weightedaverage')¶ Bases:
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.

user_index_
¶ User index.
Type: pandas.Index

item_index_
¶ Item index.
Type: pandas.Index

user_means_
¶ User mean ratings.
Type: numpy.ndarray

rating_matrix_
¶ Normalized useritem rating matrix.
Type: matrix.CSR

transpose_matrix_
¶ Transposed unnormalized rating matrix.
Type: matrix.CSR

fit
(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

load
(path)¶ Load a fit algorithm from a file. The default implementation unpickles the object and transplants its parameters and model into this object.
Parameters: file (pathlike) – the file to load.

predict_for_user
(user, items, ratings=None)¶ Compute predictions for a user and items.
Parameters:  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
(path)¶ Save a fit algorithm to a file. The default implementation pickles the object.
Parameters: file (pathlike) – the file to save.
