SurpriseWrapper¶

A wrapper to provide compatibility with surprise.

Parameters¶

• river_recommender (river.reco.base.Recommender)

Methods¶

compute_baselines

Compute users and items baselines.

The way baselines are computed depends on the bsl_options parameter passed at the creation of the algorithm (see :ref:baseline_estimates_configuration). This method is only relevant for algorithms using :func:Pearson baseline similarty<surprise.similarities.pearson_baseline> or the :class:BaselineOnly <surprise.prediction_algorithms.baseline_only.BaselineOnly> algorithm. Returns: A tuple (bu, bi), which are users and items baselines.

compute_similarities

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 :ref:similarity_measures_configuration). This method is only relevant for algorithms using a similarity measure, such as the :ref:k-NN algorithms <pred_package_knn_inpired>. Returns: The similarity matrix.

default_prediction

Used when the PredictionImpossible exception is raised during a call to :meth:predict() <surprise.prediction_algorithms.algo_base.AlgoBase.predict>. By default, return the global mean of all ratings (can be overridden in child classes).

Returns: (float): The mean of all ratings in the trainset.

estimate
fit

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. Args: trainset(:obj:Trainset <surprise.Trainset>) : A training set, as returned by the :meth:folds <surprise.dataset.Dataset.folds> method. Returns: self

Parameters

• trainset
get_neighbors

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 :ref:similarity_measures_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 :ref:k-NN algorithms <pred_package_knn_inpired>. For a usage example, see the :ref:FAQ <get_k_nearest_neighbors>. Args: iid(int): The (inner) id of the user (or item) for which we want the nearest neighbors. See :ref:this note<raw_inner_note>. k(int): The number of neighbors to retrieve. Returns: The list of the k (inner) ids of the closest users (or items) to iid.

Parameters

• iid
• k
predict

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 unknown), the prediction is set according to :meth:default_prediction() <surprise.prediction_algorithms.algo_base.AlgoBase.default_prediction>. Args: uid: (Raw) id of the user. See :ref:this note<raw_inner_note>. iid: (Raw) id of the item. See :ref:this note<raw_inner_note>. r_ui(float): The true rating :math:r_{ui}. Optional, default is None. clip(bool): Whether to clip the estimation into the rating scale. For example, if :math:\hat{r}_{ui} is :math:5.5 while the rating scale is :math:[1, 5], then :math:\hat{r}_{ui} is set to :math:5. Same goes if :math:\hat{r}_{ui} < 1. Default is True. verbose(bool): Whether to print details of the prediction. Default is False. Returns: A :obj:Prediction <surprise.prediction_algorithms.predictions.Prediction> object containing: - The (raw) user id uid. - The (raw) item id iid. - The true rating r_ui (:math:r_{ui}). - The estimated rating (:math:\hat{r}_{ui}). - Some additional details about the prediction that might be useful for later analysis.

Parameters

• uid
• iid
• r_ui – defaults to None
• clip – defaults to True
• verbose – defaults to False
test

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

Args: testset: A test set, as returned by a :ref:cross-validation itertor<use_cross_validation_iterators> or by the :meth:build_testset() <surprise.Trainset.build_testset> method. verbose(bool): Whether to print details for each predictions. Default is False. Returns: A list of :class:Prediction <surprise.prediction_algorithms.predictions.Prediction> objects that contains all the estimated ratings.

Parameters

• testset
• verbose – defaults to False