Exemple #1
0
    def transform(self, data):
        if self.mean is None or self.components is None:
            raise NotTrainedException("Train ZCA before use it.")

        data_shape = data.shape
        data = as_array2d(data)
        data_transformed = data - self.mean
        data_transformed = np.dot(data_transformed, self.components.T)
        return data_transformed.reshape(data_shape)
Exemple #2
0
    def train(self, data):
        data = as_array2d(data)
        self.mean = data.mean(axis=0)
        data = data - self.mean

        n_features = data.shape[1]
        sigma = np.dot(data.T, data) / n_features
        U, S, V = np.linalg.svd(sigma)

        self.components = (U / np.sqrt(S + self.regularization)).dot(U.T)
Exemple #3
0
    def transform(self, data):
        """ Apply ZCA transformation on data.

        Parameters
        ----------
        data : array-like

        Returns
        -------
        array-like
        """
        if self.mean is None or self.components is None:
            raise NotTrainedException("Train ZCA before use it.")

        data_shape = data.shape
        data = as_array2d(data)
        data_transformed = data - self.mean
        data_transformed = np.dot(data_transformed, self.components.T)
        return data_transformed.reshape(data_shape)
Exemple #4
0
    def transform(self, data):
        """
        Apply ZCA transformation on data.

        Parameters
        ----------
        data : array-like

        Returns
        -------
        array-like
        """
        if self.mean is None or self.components is None:
            raise NotTrainedException("Train ZCA before use it.")

        data_shape = data.shape
        data = as_array2d(data)
        data_transformed = data - self.mean
        data_transformed = np.dot(data_transformed, self.components.T)
        return data_transformed.reshape(data_shape)
Exemple #5
0
 def test_as_array2d(self):
     test_input = np.ones(5)
     actual_output = as_array2d(test_input)
     self.assertEqual((1, 5), actual_output.shape)
Exemple #6
0
 def test_as_array2d(self):
     test_input = np.ones(5)
     actual_output = as_array2d(test_input)
     self.assertEqual((1, 5), actual_output.shape)