Skip to content

Instantly share code, notes, and snippets.

@yueranyuan
Forked from dmaniry/gist:5170087
Last active December 1, 2015 04:17
Show Gist options
  • Save yueranyuan/4af84f139d33a0320447 to your computer and use it in GitHub Desktop.
Save yueranyuan/4af84f139d33a0320447 to your computer and use it in GitHub Desktop.
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

removed dependency on sklearn

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