surprise.prediction_algorithms.predictions — Surprise 1 documentation

# Source code for surprise.prediction_algorithms.predictions

    """
    The :mod:`surprise.prediction_algorithms.predictions` module defines the
    :class:`Prediction` named tuple and the :class:`PredictionImpossible`
    exception.
    """

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)

    from collections import namedtuple

    class PredictionImpossible(Exception):
        """Exception raised when a prediction is impossible.

        When raised, the estimation :math:`\hat{r}_{ui}` is set to the global mean
        of all ratings :math:`\mu`.
        """

        pass

    class Prediction(namedtuple('Prediction',
                                ['uid', 'iid', 'r_ui', 'est', 'details'])):
        """A named tuple for storing the results of a prediction.

        It's wrapped in a class, but only for documentation and printing purposes.

        Args:
            uid: The (raw) user id. See :ref:`this note<raw_inner_note>`.
            iid: The (raw) item id. See :ref:`this note<raw_inner_note>`.
            r_ui(float): The true rating :math:`r_{ui}`.
            est(float): The estimated rating :math:`\\hat{r}_{ui}`.
            details (dict): Stores additional details about the prediction that
                might be useful for later analysis.
        """

        __slots__ = ()  # for memory saving purpose.

        def __str__(self):
            s = 'user: {uid:<10} '.format(uid=self.uid)
            s += 'item: {iid:<10} '.format(iid=self.iid)
            if self.r_ui is not None:
                s += 'r_ui = {r_ui:1.2f}   '.format(r_ui=self.r_ui)
            else:
                s += 'r_ui = None   '
            s += 'est = {est:1.2f}   '.format(est=self.est)
            s += str(self.details)

            return s