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)
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)
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)
def test_as_array2d(self): test_input = np.ones(5) actual_output = as_array2d(test_input) self.assertEqual((1, 5), actual_output.shape)