コード例 #1
0
    def fit(self, X, y):
        """
        Fit the logistic regression classifier to data.
        :param X: example-by-features NumPy matrix
        :param y: example-length vector of class labels
        """
        self._means = np.mean(X, 0)
        self._stds = np.std(X, 0)

        Xold = X
        X = self._standardize_inputs(X)
        ymat = y.reshape((y.shape[0], 1))

        if self._lambda is None:
            self._lambda = internal_cross_validation(
                LogisticRegression, {'schema': self._schema}, 'lambda',
                [0, 0.001, 0.01, 0.1, 1, 10, 100], 'auc', Xold, y
            )

        optres = scipy.optimize.minimize(self.loss,
                                         np.append(self._w, [self._b]),
                                         args=(X, ymat), jac=self.jac,
                                         method='Newton-CG')

        self._w = optres.x[:-1]
        self._w = self._w.reshape((self._w.shape[0], 1))  # make it a matrix
        self._b = optres.x[-1]
コード例 #2
0
ファイル: logistic_regression.py プロジェクト: yxc775/eecs440
    def fit(self, X, y):
        """
        Fit the logistic regression classifier to data.
        :param X: example-by-features NumPy matrix
        :param y: example-length vector of class labels
        """
        self._means = np.mean(X, 0)
        self._stds = np.std(X, 0)

        Xold = X
        X = self._standardize_inputs(X)
        ymat = y.reshape((y.shape[0], 1))

        if self._lambda is None:
            self._lambda = internal_cross_validation(
                LogisticRegression, {'schema': self._schema}, 'lambda',
                [0, 0.001, 0.01, 0.1, 1, 10, 100], 'auc', Xold, y)

        optres = scipy.optimize.minimize(self.loss,
                                         np.append(self._w, [self._b]),
                                         args=(X, ymat),
                                         jac=self.jac,
                                         method='Newton-CG')

        self._w = optres.x[:-1]
        self._w = self._w.reshape((self._w.shape[0], 1))  # make it a matrix
        self._b = optres.x[-1]
コード例 #3
0
ファイル: nbayes.py プロジェクト: brenns10/eecs440
    def fit(self, X, y):
        """
        Fit Naive Bayes classifier to the given data.

        This uses the current values of self._params as our "prior estimates".
        When smoothing is used, these will be used in the m-Estimate.  Since
        the params are initialized to 1/v, this will usually be LaPlace
        smoothing, but if you were to fit this class multiple times, it would
        use the previous model as a prior estimate, which may be interesting.

        :param X: An examples-by-features NumPy matrix.
        :param y: An array of +/- 1 class labels.
        """
        # Standardize everything.
        Xstd = self._standardize_inputs(X)
        ystd = y.copy()
        ystd[y == -1] = 0
        yvals = np.bincount(ystd)

        # Select parameter m by internal cross validation.
        if self._m is None:
            self._m = internal_cross_validation(
                NaiveBayes, {'schema': self._schema}, 'm_value',
                [0, 0.001, 0.001, 0.1, 1, 10, 100], 'auc', X, y
            )

        # Set the conditional probabilities using smoothing.
        for i, matrix in enumerate(self._params):
            mp = self._m * matrix
            for yval, count in enumerate(yvals):
                Xrel = Xstd[ystd == yval]
                colbincount = np.bincount(Xrel[:, i], minlength=mp.shape[1])
                matrix[yval] = (mp[yval] + colbincount)
                matrix[yval] = matrix[yval] / (count + self._m)
            self._params[i] = matrix

        # Set the probabilities of y.
        self._yparam = yvals / len(ystd)
コード例 #4
0
    def fit(self, X, y):
        """
        Fit Naive Bayes classifier to the given data.

        This uses the current values of self._params as our "prior estimates".
        When smoothing is used, these will be used in the m-Estimate.  Since
        the params are initialized to 1/v, this will usually be LaPlace
        smoothing, but if you were to fit this class multiple times, it would
        use the previous model as a prior estimate, which may be interesting.

        :param X: An examples-by-features NumPy matrix.
        :param y: An array of +/- 1 class labels.
        """
        # Standardize everything.
        Xstd = self._standardize_inputs(X)
        ystd = y.copy()
        ystd[y == -1] = 0
        yvals = np.bincount(ystd)

        # Select parameter m by internal cross validation.
        if self._m is None:
            self._m = internal_cross_validation(
                NaiveBayes, {'schema': self._schema}, 'm_value',
                [0, 0.001, 0.001, 0.1, 1, 10, 100], 'auc', X, y)

        # Set the conditional probabilities using smoothing.
        for i, matrix in enumerate(self._params):
            mp = self._m * matrix
            for yval, count in enumerate(yvals):
                Xrel = Xstd[ystd == yval]
                colbincount = np.bincount(Xrel[:, i], minlength=mp.shape[1])
                matrix[yval] = (mp[yval] + colbincount)
                matrix[yval] = matrix[yval] / (count + self._m)
            self._params[i] = matrix

        # Set the probabilities of y.
        self._yparam = yvals / len(ystd)