Reputation: 1158
I have the following code to transform to ZCA, but I am unfamiliar with how to inverse the transform
import pandas as pd
from sklearn.linear_model import ElasticNetCV
from sklearn.datasets import make_regression
import numpy as np
def read_data():
df = pd.read_csv("https://raw.githubusercontent.com/thistleknot/Python-Stock/master/data/raw/states.csv").set_index('States')
return(df)
def whiten(X):
#just an np array
X = X.reshape((-1, np.prod(X.shape[1:])))
X_centered = X - np.mean(X, axis=0)
Sigma = np.dot(X_centered.T, X_centered) / X_centered.shape[0]
W = None
U, Lambda, _ = np.linalg.svd(Sigma)
W = np.dot(U, np.dot(np.diag(1.0 / np.sqrt(Lambda + 1e-5)), U.T))
transformed = np.dot(X_centered, W.T)
return transformed
pd.DataFrame(whiten(np.array(read_data())))
Upvotes: 0
Views: 200
Reputation: 1158
found the inverse here https://github.com/devyhia/cifar-10/blob/master/zca.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ------------------------------------
# file: zca.py
# date: Thu May 21 15:47 2015
# author:
# Maarten Versteegh
# github.com/mwv
# maartenversteegh AT gmail DOT com
#
# Licensed under GPLv3
# ------------------------------------
"""zca: ZCA whitening with a sklearn-like interface
"""
from __future__ import division
import numpy as np
from scipy import linalg
from sklearn.base import TransformerMixin, BaseEstimator
from sklearn.utils.validation import check_is_fitted
from sklearn.utils import check_array, as_float_array
class ZCA(BaseEstimator, TransformerMixin):
def __init__(self, regularization=1e-6, copy=False):
self.regularization = regularization
self.copy = copy
def fit(self, X, y=None):
"""Compute the mean, whitening and dewhitening matrices.
Parameters
----------
X : array-like with shape [n_samples, n_features]
The data used to compute the mean, whitening and dewhitening
matrices.
"""
X = check_array(X, accept_sparse=None, copy=self.copy,
ensure_2d=True)
X = as_float_array(X, copy=self.copy)
self.mean_ = X.mean(axis=0)
X_ = X - self.mean_
cov = np.dot(X_.T, X_) / (X_.shape[0]-1)
U, S, _ = linalg.svd(cov)
s = np.sqrt(S.clip(self.regularization))
s_inv = np.diag(1./s)
s = np.diag(s)
self.whiten_ = np.dot(np.dot(U, s_inv), U.T)
self.dewhiten_ = np.dot(np.dot(U, s), U.T)
return self
def transform(self, X, y=None, copy=None):
"""Perform ZCA whitening
Parameters
----------
X : array-like with shape [n_samples, n_features]
The data to whiten along the features axis.
"""
check_is_fitted(self, 'mean_')
X = as_float_array(X, copy=self.copy)
return np.dot(X - self.mean_, self.whiten_.T)
def inverse_transform(self, X, copy=None):
"""Undo the ZCA transform and rotate back to the original
representation
Parameters
----------
X : array-like with shape [n_samples, n_features]
The data to rotate back.
"""
check_is_fitted(self, 'mean_')
X = as_float_array(X, copy=self.copy)
return np.dot(X, self.dewhiten_) + self.mean_
Upvotes: 1