<jupyter_start><jupyter_text>Assignment 7 (PCA) 201916006<jupyter_code>import h5py
import numpy as np
import pandas as pd
path = 'faceimages.mat'
arrays = {}
input_file = h5py.File(path, 'r')
mat = {k:v for k,v in input_file.items() if k[0] != '_'}
mat1 = mat['data']
data = pd.DataFrame({k: pd.Series(v[0]) for k,v in mat.items()})
df1 = pd.DataFrame(mat1).T
df1 
# Mean-centering the data
for c in df1:
    m = df1[c].mean()
    df1[c] = df1[c] - m
df1
df2 = np.array(df1)
# Co-variance of the data matrix
cov = np.cov(df2.T)
cov.shape
print(cov)
#Eigen Values & Vectors from covariance matrix
eigenValues, eigenVectors = np.linalg.eig(cov)
print("Eigen Values:  {} \n".format(eigenValues))
print("Eigen Vectors: {} \n".format(eigenVectors))
# initializing the pca
import matplotlib.pyplot as plt
from sklearn import decomposition
pca = decomposition.PCA()
# PCA for dimensionality redcution
pca_data = pca.fit_transform(df2)

percentage_var_explained = pca.explained_variance_ / np.sum(pca.explained_variance_);
cum_var_explained = np.cumsum(percentage_var_explained)

# Plot the energy curve of PCA
plt.figure(1, figsize=(6, 4))
plt.clf()
plt.plot(cum_var_explained, linewidth=2)
plt.axis('tight')
plt.grid()
plt.xlabel('n_components')
plt.ylabel('Cumulative_explainedVariance')
plt.show()

# If we take 200-dimensions, approx. 95% of variance is explained.
# Projection matrix with 300 pricipal components
pca.n_components = 300
lowdimdata = pca.fit_transform(df2)
lowdimdata_df = pd.DataFrame(data = lowdimdata)
lowdimdata_df
# Reconstruction error for 300 principal components
from sklearn.metrics import r2_score
from sklearn.metrics import mean_squared_error
from math import sqrt
import numpy as np
r2 = r2_score(df2[:,:300],lowdimdata)
rmse = sqrt(mean_squared_error(df2[:,:300],lowdimdata))
rmse
# Projection matrix with 5 principal components
pca.n_components = 5
lowdimdata = pca.fit_transform(df2)
lowdimdata_df = pd.DataFrame(data = lowdimdata)
lowdimdata_df
# Reconstruction error for 5 principal components
r2 = r2_score(data2[:,:5],lowdimdata)
rmse = sqrt(mean_squared_error(data2[:,:5],lowdimdata))
rmse
# Projection matrix with 10 principal components
pca.n_components = 10
lowdimdata = pca.fit_transform(df2)
lowdimdata_df = pd.DataFrame(data = lowdimdata)
lowdimdata_df
# Reconstruction error for 10 principal components
r2 = r2_score(data2[:,:10],lowdimdata)
rmse = sqrt(mean_squared_error(df2[:,:10],lowdimdata))
rmse
# Projection matrix with 50 principal components
pca.n_components = 50
lowdimdata = pca.fit_transform(df2)
lowdimdata_df = pd.DataFrame(data = lowdimdata)
lowdimdata_df
# Reconstruction error for 50 principal components
r2 = r2_score(data2[:,:50],lowdimdata)
rmse = sqrt(mean_squared_error(df2[:,:50],lowdimdata))
rmse
# Projection matrix with 100 principal components
pca.n_components = 100
lowdimdata = pca.fit_transform(df2)
lowdimdata_df = pd.DataFrame(data = lowdimdata)
lowdimdata_df
# Reconstruction error for 100 principal components
r2 = r2_score(data2[:,:100],lowdimdata)
rmse = sqrt(mean_squared_error(df2[:,:100],lowdimdata))
rmse<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11524879614767256, 'fraction_numerical': 0.03756019261637239, 'mean_word_length': 6.581508515815085, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '553284', 'license': 'MIT', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1140, 'n_words': 270, 'path': 'lab/Assignments/IT542_Assignment_7/201916006_PRML_Assignment7.ipynb', 'repo_name': 'mayank1101/Pattern-Recognition-and-Machine-Learning-PRML-', 'size': '3115'}