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 behaviour 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 algoritihm (see Baselines estimates configuration).

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

Build the simlarity matrix.

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

Returns:The similarity matrix.
predict(uid, iid, r_ui, 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 (for whatever reason), 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}\).
  • 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.

test(testset, verbose=False)

Test the algorithm on given testset.

  • testset – A test set, as returned by the folds method.
  • verbose (bool) – Whether to print details for each predictions. Default is False.

A list of Prediction



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.