예제 #1
0
    def fit(self, trainset):
        """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.
        """

        # Check if train method is overridden: this means the object is an old
        # style algo (new algo only have fit() so self.__class__.train will be
        # AlgoBase.train). If true, there are 2 possible cases:
        # - algo.fit() was called. In this case algo.train() was skipped which
        #   is bad. We call it and skip this part next time we enter fit().
        #   Then return immediatly because fit() has already been called by
        #   AlgoBase.train() (which has been called by algo.train()).
        # - algo.train() was called, which is the old way. In that case,
        #   the skip flag will ignore this.
        # This is fairly ugly and hacky but I did not find anything better so
        # far, in order to maintain backward compatibility... See
        # tests/test_train2fit.py for supported cases.
        if (guf(self.__class__.train) is not guf(AlgoBase.train)
                and not self.skip_train):
            self.train(trainset)
            return
        self.skip_train = False

        self.trainset = trainset

        # (re) Initialise baselines
        self.bu = self.bi = None
예제 #2
0
    def __init__(self, **kwargs):

        self.bsl_options = kwargs.get('bsl_options', {})
        self.sim_options = kwargs.get('sim_options', {})
        if 'user_based' not in self.sim_options:
            self.sim_options['user_based'] = True
        self.skip_train = False

        if (guf(self.__class__.fit) is guf(AlgoBase.fit) and
           guf(self.__class__.train) is not guf(AlgoBase.train)):
            warnings.warn('It looks like this algorithm (' +
                          str(self.__class__) +
                          ') implements train() '
                          'instead of fit(): train() is deprecated, '
                          'please use fit() instead.', UserWarning)
예제 #3
0
    def __init__(self, **kwargs):

        self.bsl_options = kwargs.get('bsl_options', {})
        self.sim_options = kwargs.get('sim_options', {})
        if 'user_based' not in self.sim_options:
            self.sim_options['user_based'] = True
        self.skip_train = False

        if (guf(self.__class__.fit) is guf(AlgoBase.fit) and
           guf(self.__class__.train) is not guf(AlgoBase.train)):
            warnings.warn('It looks like this algorithm (' +
                          str(self.__class__) +
                          ') implements train() '
                          'instead of fit(): train() is deprecated, '
                          'please use fit() instead.', UserWarning)
예제 #4
0
    def __init__(self, **kwargs):

        # bsl_options 配置迭代优化算法的类型 als或者sgd,如果选择对应的算法,还要给出对应
        # 的算法参数
        self.bsl_options = kwargs.get('bsl_options', {})
        # (物品/用户)相似度计算 配置
        self.sim_options = kwargs.get('sim_options', {})
        if 'user_based' not in self.sim_options:
            self.sim_options['user_based'] = True  # 默认使用 base user计算
        self.skip_train = False

        if (guf(self.__class__.fit) is guf(AlgoBase.fit) and
           guf(self.__class__.train) is not guf(AlgoBase.train)):
            warnings.warn('It looks like this algorithm (' +
                          str(self.__class__) +
                          ') implements train() '
                          'instead of fit(): train() is deprecated, '
                          'please use fit() instead.', UserWarning)
예제 #5
0
    def fit(self, trainset):
        """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
        """

        # Check if train method is overridden: this means the object is an old
        # style algo (new algo only have fit() so self.__class__.train will be
        # AlgoBase.train). If true, there are 2 possible cases:
        # - algo.fit() was called. In this case algo.train() was skipped which
        #   is bad. We call it and skip this part next time we enter fit().
        #   Then return immediatly because fit() has already been called by
        #   AlgoBase.train() (which has been called by algo.train()).
        # - algo.train() was called, which is the old way. In that case,
        #   the skip flag will ignore this.
        # This is fairly ugly and hacky but I did not find anything better so
        # far, in order to maintain backward compatibility... See
        # tests/test_train2fit.py for supported cases.
        if (guf(self.__class__.train) is not guf(AlgoBase.train) and
                not self.skip_train):
            self.train(trainset)
            return
        self.skip_train = False

        self.trainset = trainset

        # (re) Initialise baselines
        self.bu = self.bi = None

        return self