Example #1
0
 def normal(self, X, y):
     """Use normal equation regularized to compute the parameters."""
     X = features_reshape(X)
     X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)
     L = np.identity(X.shape[0])
     L[0, 0] = 0
     self.optimizer.parameters = (
         np.linalg.pinv(X.T.dot(X) + self.regularization * L).dot(X.T).dot(y)
     )
Example #2
0
    def predict_probability(self, X):
        """Predict the probability of a target given features."""
        if self.parameters is None or self.labels is None:
            raise NotFitted("the model must be fitted before usage")

        X = features_reshape(X)
        if self.standardize is not None:
            X = self.standardize(X)
        X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)
        return self.hypothesis(X, self.parameters)
Example #3
0
    def fit(self, X, y):
        """Fit the model."""
        X = features_reshape(X)
        if not check_dataset_consistency(X, y):
            raise InvalidInput("the features set and target set must have as many rows")

        if self.standardize is not None:
            X = self.standardize(X)
        X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)
        self.optimizer(X, y)
Example #4
0
    def score_samples(self, X):
        """Compute the log-likelihood of all samples."""
        if self._U_reduced is None or self._mean is None:
            raise NotFitted("the model must be fitted before usage")

        X = features_reshape(X)

        n_features = X.shape[1]
        precision = np.linalg.inv(self._covariance_matrix)
        residuals = X - self._mean

        return -(1 / 2) * (-np.log(np.linalg.det(precision)) + np.sum(
            (residuals * np.dot(residuals, precision)), axis=1) +
                           n_features * np.log(2 * np.pi))
Example #5
0
    def fit(self, X):
        """Train the model."""
        X = features_reshape(X)
        max_dimension = min(X.shape)

        if self.n_components is None:
            self.n_components = max_dimension
        elif self.n_components > max_dimension:
            raise InvalidInput(
                f"n_components must be lesser than {max_dimension}")

        self._covariance_matrix = np.cov(X.T)
        self._mean = np.mean(X, axis=0)

        U, _, _ = np.linalg.svd(self._covariance_matrix)
        self._U_reduced = U[:, :self.n_components]
Example #6
0
    def fit(self, X, y):
        """Fit the model."""
        X = features_reshape(X)
        if not check_dataset_consistency(X, y):
            raise InvalidInput(
                "the features set and target set must have as many rows")

        if self.standardize is not None:
            X = self.standardize(X)
        X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)

        self.labels = np.unique(y)
        n_labels = np.size(self.labels)
        if n_labels < 2:
            raise InvalidInput(
                "target must have at least two different classes")
        elif n_labels == 2:
            self.optimizer(X, y)
        else:
            self.optimizer(X, (y == self.labels).astype(int))
Example #7
0
 def normal(self, X, y):
     """Use normal equation to compute the parameters."""
     X = features_reshape(X)
     X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)
     self.optimizer.parameters = np.linalg.pinv(X.T.dot(X)).dot(X.T).dot(y)