lenskit.knn.user.UserKNNScorer#
- class lenskit.knn.user.UserKNNScorer(config=None, **kwargs)#
Bases:
lenskit.pipeline.Component[lenskit.data.ItemList],lenskit.training.TrainableUser-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.
Note
This component must be used with queries containing the user’s history, either directly in the input or by wiring its query input to the output of a user history component (e.g.,
UserTrainingHistoryLookup).- Stability:
- Caller (see Stability Levels).
- Parameters:
config (object | None)
kwargs (Any)
- config: UserKNNConfig#
The component configuration object. Component classes that support configuration must redefine this attribute with their specific configuration class type, which can be a Python dataclass or a Pydantic model class.
- users: lenskit.data.Vocabulary#
The index of user IDs.
- items: lenskit.data.Vocabulary#
The index of item IDs.
- user_means: numpy.ndarray[tuple[int], numpy.dtype[numpy.float32]] | None#
Mean rating for each known user.
- user_vectors: scipy.sparse.csr_array#
Normalized rating matrix (CSR) to find neighbors at prediction time.
- user_ratings: lenskit.data.matrix.SparseRowArray#
Centered but un-normalized rating matrix (COO) to find neighbor ratings.
- is_trained()#
Query if this component has already been trained.
- train(data, options=TrainingOptions())#
“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.
data (lenskit.data.Dataset)
options (lenskit.training.TrainingOptions)
- __call__(query, items)#
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.
query (lenskit.data.QueryInput)
- Returns:
scores for the items, indexed by item id.
- Return type: