Covariance Visualizations


Visualizing Covariances in the process of whitening data

Load data


In [2]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":
    # Load data
    if ('trainingFaces' not in globals()):  # In ipython, use "run -i homework2_template.py" to avoid re-loading of data
        trainingFaces = np.load("FacesData/trainingFaces.npy")
        trainingLabels = np.load("FacesData/trainingLabels.npy")
        testingFaces = np.load("FacesData/testingFaces.npy")
        testingLabels = np.load("FacesData/testingLabels.npy")

Find covariance of training data and visualize the covariance matrix


In [3]:
X = trainingFaces
cov = np.cov(X.T)
small_constant = 1e-2
cov_added = cov + (small_constant * np.eye(cov.shape[0]))

img = plt.imshow(cov_added)
plt.title('Covariance matrix')
plt.show(img)

V, U = np.linalg.eig(cov_added) #Here U is the eigenvectors of covariance and V is the eigenvalues

#sort eigenvectors
idx = V.argsort()[::-1]   
V  = V[idx]
U  = U[:,idx]
    
diag_mat = np.diag(1. / np.sqrt(V+small_constant))

plt.title('Eigenvalue diagonal matrix')
img = plt.imshow(np.real(diag_mat))
plt.show(img)

eig_cov = np.real(np.cov(U.T))

plt.title('Eigenvector covariance matrix')
img = plt.imshow(eig_cov)
plt.show(img)



In [4]:
diag = np.diag(V)
iden = np.real(diag_mat.dot(diag.dot(diag_mat)))
diag_iden = [ iden[i][i] for i in range(len(iden))]
plt.title('Identity matrix obtained from gamma^(-1/2)*gamma*gamma^(-1/2)')
img = plt.imshow(iden)
plt.show(img)


Create whintening transformation and whiten data


In [5]:
# white_transform = np.dot(np.dot(U, diag_mat), U.T)
white_transform = np.dot(U, diag_mat)
whitened = np.dot(X,white_transform)
cov_whitened = np.real(np.cov(whitened.T))

In [6]:
plt.title('Whitened training faces covariance matrix')
img = plt.imshow(cov_whitened)
plt.show(img)



In [7]:
diag_whitened_cov = [ cov_whitened[i][i] for i in range(len(cov_whitened))]

In [9]:
iden_lhs = np.real(diag_mat.dot(U.T.dot(cov.dot(U.dot(diag_mat)))))
diag_iden_lhs = [ iden_lhs[i][i] for i in range(len(iden_lhs))]
plt.title('Identity matrix obtained from LHS of gamma^(-1/2)*gamma*gamma^(-1/2)')
img = plt.imshow(iden_lhs)
plt.show(img)



In [ ]: