Пример #1
0
    def fit(self, X, y):
        """ Fit model with specified loss.

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

        y : float | ndarray, shape = (n_samples, )

                the targets have to be encodes as {-1, 1}.
        """
        y = _validate_class_labels(y)
        self.classes_ = np.unique(y)
        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.copy()
        i_class1 = (y_train == self.classes_[0])
        y_train[i_class1] = -1
        y_train[-i_class1] = 1

        check_consistent_length(X, y)
        y = y.astype(np.float64)
        X = X.T
        X = check_array(X, accept_sparse="csc", dtype=np.float64)

        self.w0_, self.w_, self.V_ = ffm.ffm_sgd_fit(self, X, y)
        return self
Пример #2
0
    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