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_optionsparameter 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_optionsparameter 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_optionsparameter 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
predictmethod converts raw ids to inner ids and then calls theestimatemethod 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: Returns: A
Predictionobject.
-
test(testset, verbose=False)[source]¶ Test the algorithm on given testset.
Parameters: - testset – A test set, as returned by the
foldsmethod. - verbose (bool) – Whether to print details for each predictions. Default is False.
Returns: A list of
Predictionobjects.- testset – A test set, as returned by the
-
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 thefoldsmethod.
-