lenskit.knn.user#

User-based k-NN collaborative filtering.

Classes#

UserKNNConfig

Configuration for ItemKNNScorer.

UserKNNScorer

User-user nearest-neighbor collaborative filtering with ratings. This

Module Contents#

class lenskit.knn.user.UserKNNConfig#

Bases: pydantic.BaseModel

Configuration for ItemKNNScorer.

max_nbrs: pydantic.PositiveInt#

The maximum number of neighbors for scoring each item.

min_nbrs: pydantic.PositiveInt = 1#

The minimum number of neighbors for scoring each item.

min_sim: pydantic.PositiveFloat = 1e-06#

Minimum similarity threshold for considering a neighbor. Must be positive; if less than the smallest 32-bit normal (\(1.175 \times 10^{-38}\)), is clamped to that value.

feedback: lenskit.data.FeedbackType = 'explicit'#

The type of input data to use (explicit or implicit). This affects data pre-processing and aggregation.

static clamp_min_sim(sim)#
Return type:

float

property explicit: bool#

Query whether this is in explicit-feedback mode.

Return type:

bool

class lenskit.knn.user.UserKNNScorer(config=None, **kwargs)#

Bases: lenskit.pipeline.Component[lenskit.data.ItemList], lenskit.training.Trainable

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.

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:
__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:

pandas.Series

Exported Aliases#

class lenskit.knn.user.Dataset#

Re-exported alias for lenskit.data.Dataset.

lenskit.knn.user.FeedbackType#

Re-exported alias for lenskit.data.FeedbackType.

class lenskit.knn.user.ItemList#

Re-exported alias for lenskit.data.ItemList.

lenskit.knn.user.QueryInput#

Re-exported alias for lenskit.data.QueryInput.

class lenskit.knn.user.RecQuery#

Re-exported alias for lenskit.data.RecQuery.

class lenskit.knn.user.Vocabulary#

Re-exported alias for lenskit.data.Vocabulary.

class lenskit.knn.user.SparseRowArray#

Re-exported alias for lenskit.data.matrix.SparseRowArray.

exception lenskit.knn.user.DataWarning#

Re-exported alias for lenskit.diagnostics.DataWarning.

class lenskit.knn.user.Stopwatch#

Re-exported alias for lenskit.logging.Stopwatch.

lenskit.knn.user.get_logger()#

Re-exported alias for lenskit.logging.get_logger().

lenskit.knn.user.ensure_parallel_init()#

Re-exported alias for lenskit.parallel.config.ensure_parallel_init().

class lenskit.knn.user.Component#

Re-exported alias for lenskit.pipeline.Component.

class lenskit.knn.user.Trainable#

Re-exported alias for lenskit.training.Trainable.

class lenskit.knn.user.TrainingOptions#

Re-exported alias for lenskit.training.TrainingOptions.