コード例 #1
0
ファイル: mcmc.py プロジェクト: ibayer/fastFM
    def fit_predict(self, X_train, y_train, X_test, n_more_iter=0):
        """Return average of posterior estimates of the test samples.

        Parameters
        ----------
        X_train : scipy.sparse.csc_matrix, (n_samples, n_features)

        y_train : array, shape (n_samples)

        X_test : scipy.sparse.csc_matrix, (n_test_samples, n_features)

        n_more_iter : int
                Number of iterations to continue from the current Coefficients.

        Returns
        -------
        T : array, shape (n_test_samples)
        """
        self.task = "regression"
        X_train, y_train, X_test = _validate_mcmc_fit_input(X_train, y_train,
                                                            X_test)

        self.n_iter = self.n_iter + n_more_iter

        if n_more_iter > 0:
            _check_warm_start(self, X_train)
            assert self.prediction_.shape[0] == X_test.shape[0]
            assert self.hyper_param_.shape
            self.warm_start = True
        else:
            self.iter_count = 0

        coef, y_pred = ffm.ffm_mcmc_fit_predict(self, X_train,
                                                X_test, y_train)
        self.w0_, self.w_, self.V_ = coef
        self.prediction_ = y_pred
        self.warm_start = False

        if self.iter_count != 0:
            self.iter_count = self.iter_count + n_more_iter
        else:
            self.iter_count = self.n_iter

        return y_pred
コード例 #2
0
ファイル: mcmc.py プロジェクト: sebkur/fastFM
    def fit_predict(self, X_train, y_train, X_test, n_more_iter=0):
        """Return average of posterior estimates of the test samples.

        Parameters
        ----------
        X_train : scipy.sparse.csc_matrix, (n_samples, n_features)

        y_train : array, shape (n_samples)

        X_test : scipy.sparse.csc_matrix, (n_test_samples, n_features)

        n_more_iter : int
                Number of iterations to continue from the current Coefficients.

        Returns
        ------

        T : array, shape (n_test_samples)
        """
        self.task = "regression"
        X_train, y_train, X_test = _validate_mcmc_fit_input(
            X_train, y_train, X_test)

        self.n_iter = self.n_iter + n_more_iter

        if n_more_iter > 0:
            _check_warm_start(self, X_train)
            assert self.prediction_.shape[0] == X_test.shape[0]
            assert self.hyper_param_.shape
            self.warm_start = True
        else:
            self.iter_count = 0

        coef, y_pred = ffm.ffm_mcmc_fit_predict(self, X_train, X_test, y_train)
        self.w0_, self.w_, self.V_ = coef
        self.prediction_ = y_pred
        self.warm_start = False

        if self.iter_count != 0:
            self.iter_count = self.iter_count + n_more_iter
        else:
            self.iter_count = self.n_iter

        return y_pred
コード例 #3
0
ファイル: mcmc.py プロジェクト: ibayer/fastFM
    def fit_predict_proba(self, X_train, y_train, X_test):
        """Return average class probabilities of posterior estimates of the
        test samples.
        Use only with MCMC!

        Parameters
        ----------
        X_train : scipy.sparse.csc_matrix, (n_samples, n_features)

        y_train : array, shape (n_samples)
                the targets have to be encodes as {-1, 1}.

        X_test : scipy.sparse.csc_matrix, (n_test_samples, n_features)

        Returns
        -------
        y_pred : array, shape (n_test_samples)
            Returns probability estimates for the class with lowest
            classification label.

        """
        self.task = "classification"

        self.classes_ = np.unique(y_train)
        if len(self.classes_) != 2:
            raise ValueError("This solver only supports binary classification"
                             " but the data contains"
                             " class: %r" % self.classes_)

        # fastFM-core expects labels to be in {-1,1}
        y_train = y_train.copy()
        i_class1 = (y_train == self.classes_[0])
        y_train[i_class1] = -1
        y_train[~i_class1] = 1

        X_train, y_train, X_test = _validate_mcmc_fit_input(X_train, y_train,
                                                            X_test)
        y_train = _validate_class_labels(y_train)

        coef, y_pred = ffm.ffm_mcmc_fit_predict(self, X_train,
                                                X_test, y_train)
        self.w0_, self.w_, self.V_ = coef
        return y_pred
コード例 #4
0
ファイル: mcmc.py プロジェクト: sebkur/fastFM
    def fit_predict_proba(self, X_train, y_train, X_test):
        """Return average class probabilities of posterior estimates of the
        test samples.
        Use only with MCMC!

        Parameters
        ----------
        X_train : scipy.sparse.csc_matrix, (n_samples, n_features)

        y_train : array, shape (n_samples)
                the targets have to be encodes as {-1, 1}.

        X_test : scipy.sparse.csc_matrix, (n_test_samples, n_features)

        Returns
        ------

        y_pred : array, shape (n_test_samples)
            Returns probability estimates for the class with lowest
            classification label.

        """
        self.task = "classification"

        self.classes_ = np.unique(y_train)
        if len(self.classes_) != 2:
            raise ValueError("This solver only supports binary classification"
                             " but the data contains"
                             " class: %r" % self.classes_)

        # fastFM-core expects labels to be in {-1,1}
        y_train = y_train.copy()
        i_class1 = (y_train == self.classes_[0])
        y_train[i_class1] = -1
        y_train[-i_class1] = 1

        X_train, y_train, X_test = _validate_mcmc_fit_input(
            X_train, y_train, X_test)
        y_train = _validate_class_labels(y_train)

        coef, y_pred = ffm.ffm_mcmc_fit_predict(self, X_train, X_test, y_train)
        self.w0_, self.w_, self.V_ = coef
        return y_pred