Skip to content

Instantly share code, notes, and snippets.

@puraminy
Last active December 30, 2019 12:38
Show Gist options
  • Save puraminy/0fcdedb1d187ff22be3b383349de8619 to your computer and use it in GitHub Desktop.
Save puraminy/0fcdedb1d187ff22be3b383349de8619 to your computer and use it in GitHub Desktop.
Eigen vector Whitening
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from numpy import array
from numpy import mean
from numpy import cov
from numpy.linalg import eig, svd
path = 'drive/My Drive/FMNIST/'
x_train = pd.read_csv(path+'Train_Data.csv')
y_train = pd.read_csv(path+'Train_Labels.csv')
x_test = pd.read_csv(path+'Test_Data.csv')
y_test = pd.read_csv(path+'Test_Labels.csv')
# merging train and test samples
x_train.columns = range(182)
x_test.columns = range(182)
X = pd.concat([x_train, x_test])
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from numpy import array
from numpy import mean
from numpy import cov
from numpy.linalg import eig, svd
# X = np.array([[1,2],[2,4],[3,6]])
# X = pd.DataFrame(X)
X.plot.scatter(x=0,y=1)
plt.savefig("X.png")
print("================== Whitening using Eigen Values ===================")
M = mean(X.T, axis=1)
# center columns by subtracting column means
C = X - M
# calculate covariance matrix of centered matrix
X_cov = np.dot(C.T,C)/len(C)
# get the covariance matrix using numpy library
# X_cov = cov(X, rowvar=False, bias=True)
# eigenvalue decomposition of the covariance matrix
d, V = np.linalg.eigh(X_cov)
print("d=", d)
colinear = False
for v in d:
if v < 1E-5:
colinear = True
if colinear:
print("\nlinear correlatin was found\n\n")
else:
print("\nNo linear correlatin was found\n\n")
D = np.diag(1. / np.sqrt(d+1E-18))
W = (D.dot(V.T))
X_white = (W.dot(C.T)).T
fig, ax = plt.subplots(figsize=(10,10))
plt.gca().set_aspect('equal', adjustable='box')
# X_white = pd.DataFrame(X_white)
# X_white.plot.scatter(x=0,y=1)
ax.scatter(X_white[:,0], X_white[:,1], c='g')
ax.set_title("Whitened")
fig.savefig('w.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment