Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@yueranyuan
Copy link
Author

yueranyuan commented Dec 1, 2015

removed dependency on sklearn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment