The functions in
lenskit.batch enable you to generate many recommendations or
predictions at the same time, useful for evaluations and experiments.
recommend(algo, model, users, n, candidates, ratings=None, nprocs=None)¶
Batch-recommend for multiple users. The provided algorithm should be a
algorithms.Predictor(which will be converted to a top-N recommender).
- algo – the algorithm
- model – The algorithm model
- users (array-like) – the users to recommend for
- n (int) – the number of recommendations to generate (None for unlimited)
- candidates – the users’ candidate sets. This can be a function, in which case it will be passed each user ID; it can also be a dictionary, in which case user IDs will be looked up in it.
- ratings (pandas.DataFrame) – if not
None, a data frame of ratings to attach to recommendations when available.
A frame with at least the columns
item; possibly also
score, and any other columns returned by the recommender.
predict(algo, model, pairs, nprocs=None)¶
Generate predictions for user-item pairs. The provided algorithm should be a
algorithms.Predictoror a function of two arguments: the user ID and a list of item IDs. It should return a dictionary or a
pandas.Seriesmapping item IDs to predictions.
- algo (lenskit.algorithms.Predictor) – A rating predictor function or algorithm.
- model (any) – A model for the algorithm.
- pairs (pandas.DataFrame) – A data frame of (
item) pairs to predict for. If this frame also contains a
ratingcolumn, it will be included in the result.
- nprocs (int) – The number of processes to use for parallel batch prediction.
a frame with columns
predictioncontaining the prediction results. If
pairscontains a rating column, this result will also contain a rating column.
MultiEval(path, predict=True, recommend=100, candidates=<class 'lenskit.topn.UnratedCandidates'>, nprocs=None)¶
A runner for carrying out multiple evaluations, such as parameter sweeps.
- path (str or
pathlib.Path) – the working directory for this evaluation. It will be created if it does not exist.
- predict (bool) – whether to generate rating predictions.
- recommend (int) – the number of recommendations to generate per user (None to disable top-N).
- candidates (function) – the default candidate set generator for recommendations. It should take the training data and return a candidate generator, itself a function mapping user IDs to candidate sets.
add_algorithms(algos, parallel=False, attrs=, **kwargs)¶
Add one or more algorithms to the run.
- algos (algorithm or list) – the algorithm(s) to add.
- parallel (bool) – if
True, allow this algorithm to be trained in parallel with others.
- attrs (list of str) – a list of attributes to extract from the algorithm objects and include in the run descriptions.
- kwargs – additional attributes to include in the run descriptions.
add_datasets(data, name=None, candidates=None, **kwargs)¶
Add one or more datasets to the run.
- data –
The input data set(s) to run. Can be one of the following:
- A tuple of (train, test) data.
- An iterable of (train, test) pairs, in which case the iterable is not consumed until it is needed.
- A function yielding either of the above, to defer data load until it is needed.
Data can be either data frames or paths; paths are loaded after detection using :py:fun:`util.read_df_detect`.
- kwargs – additional attributes pertaining to these data sets.
- data –
Persist the data for an experiment, replacing in-memory data sets with file names. Once this has been called, the sweep can be pickled.
Run the evaluation.
- path (str or