-
-
Save yueranyuan/4af84f139d33a0320447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from scipy import linalg | |
class ZCA(): | |
""" | |
Performs ZCA. Based on | |
https://gist.github.com/duschendestroyer/5170087 | |
""" | |
def __init__(self, regularization=10**-5): | |
self.regularization = regularization | |
def fit(self, X): | |
X = np.array(X) | |
X = X.astype(np.float32) | |
self._mean = np.mean(X, axis=0) | |
X -= self._mean | |
sigma = np.dot(X.T,X) / X.shape[1] | |
U, S, V = linalg.svd(sigma) | |
tmp = np.dot(U, np.diag(1/np.sqrt(S+self.regularization))) | |
self._components = np.dot(tmp, U.T) | |
return self | |
def transform(self, X): | |
X = np.array(X) | |
X = X.astype(np.float32) | |
X_transformed = X - self._mean | |
X_transformed = np.dot(X_transformed, self._components.T) | |
return X_transformed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
removed dependency on sklearn