The algorithm base class

The surprise.prediction_algorithms.algo_base module defines the base class AlgoBase from which every single prediction algorithm has to inherit.

class surprise.prediction_algorithms.algo_base.AlgoBase(**kwargs)

Abstract class where is defined the basic behavior of a prediction algorithm.

Keyword Arguments:
 baseline_options (dict, optional) – If the algorithm needs to compute a baseline estimate, the baseline_options parameter is used to configure how they are computed. See Baselines estimates configuration for usage.

Compute users and items baselines.

The way baselines are computed depends on the bsl_options parameter passed at the creation of the algorithm (see Baselines estimates configuration).

This method is only relevant for algorithms using Pearson baseline similarty or the BaselineOnly algorithm.

Returns:A tuple (bu, bi), which are users and items baselines.

Build the similarity matrix.

The way the similarity matrix is computed depends on the sim_options parameter passed at the creation of the algorithm (see Similarity measure configuration).

This method is only relevant for algorithms using a similarity measure, such as the k-NN algorithms.

Returns:The similarity matrix.

Train an algorithm on a given training set.

This method is called by every derived class as the first basic step for training an algorithm. It basically just initializes some internal structures and set the self.trainset attribute.

Parameters:trainset (Trainset) – A training set, as returned by the folds method.
get_neighbors(iid, k)

Return the k nearest neighbors of iid, which is the inner id of a user or an item, depending on the user_based field of sim_options (see Similarity measure configuration).

As the similarities are computed on the basis of a similarity measure, this method is only relevant for algorithms using a similarity measure, such as the k-NN algorithms.

For a usage example, see the FAQ.

  • iid (int) – The (inner) id of the user (or item) for which we want the nearest neighbors. See this note.
  • k (int) – The number of neighbors to retrieve.

The list of the k (inner) ids of the closest users (or items) to iid.

predict(uid, iid, r_ui=None, clip=True, verbose=False)

Compute the rating prediction for given user and item.

The predict method converts raw ids to inner ids and then calls the estimate method which is defined in every derived class. If the prediction is impossible (e.g. because the user and/or the item is unkown), the prediction is set to the global mean of all ratings.

  • uid – (Raw) id of the user. See this note.
  • iid – (Raw) id of the item. See this note.
  • r_ui (float) – The true rating \(r_{ui}\). Optional, default is None.
  • clip (bool) – Whether to clip the estimation into the rating scale. For example, if \(\hat{r}_{ui}\) is \(5.5\) while the rating scale is \([1, 5]\), then \(\hat{r}_{ui}\) is set to \(5\). Same goes if \(\hat{r}_{ui} < 1\). Default is True.
  • verbose (bool) – Whether to print details of the prediction. Default is False.

A Prediction object containing:

  • The (raw) user id uid.
  • The (raw) item id iid.
  • The true rating r_ui (\(\hat{r}_{ui}\)).
  • The estimated rating (\(\hat{r}_{ui}\)).
  • Some additional details about the prediction that might be useful for later analysis.

test(testset, verbose=False)

Test the algorithm on given testset, i.e. estimate all the ratings in the given testset.


A list of Prediction objects that contains all the estimated ratings.


Deprecated method: use fit() instead.