The algorithm base class

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

class surprise.prediction_algorithms.algo_base.AlgoBase(**kwargs)[source]

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_baselines()[source]

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.
compute_similarities()[source]

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=0, verbose=False)[source]

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. Also, if \(\hat{r}_{ui}\) is outside the bounds of the rating scale, (e.g. \(\hat{r}_{ui} = 6\) for a rating scale of \([1, 5]\)), then it is capped.

Parameters:
  • uid – (Raw) id of the user. See this note.
  • iid – (Raw) id of the item. See this note.
  • r – The true rating \(r_{ui}\).
  • verbose (bool) – Whether to print details of the prediction. Default is False.
Returns:

A Prediction object.

test(testset, verbose=False)[source]

Test the algorithm on given testset.

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

A list of Prediction objects.

train(trainset)[source]

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.