Last active
August 17, 2021 14:06
-
-
Save dmaniry/5170087 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 | |
from sklearn.utils import array2d, as_float_array | |
from sklearn.base import TransformerMixin, BaseEstimator | |
class ZCA(BaseEstimator, TransformerMixin): | |
def __init__(self, regularization=10**-5, copy=False): | |
self.regularization = regularization | |
self.copy = copy | |
def fit(self, X, y=None): | |
X = array2d(X) | |
X = as_float_array(X, copy = self.copy) | |
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 = array2d(X) | |
X_transformed = X - self.mean_ | |
X_transformed = np.dot(X_transformed, self.components_.T) | |
return X_transformed |
if you got the code from https://github.com/ltrottier/ZCA-Whitening-Python/blob/master/zca.py plz cite
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if you are using numpy it is very easy just use https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.reshape.html