<jupyter_start><jupyter_text>ML Programming Part Submitted By: <NAME>(as11500), <NAME>(ddp337)  Question 2 Eigen Value and Vector<jupyter_code>import numpy as np
A = [[0,14],[6,9]]
w,v = np.linalg.eig(A)
print("Eigen Values:",w)
print("Eigen Vectors:")
print(v)<jupyter_output>Eigen Values: [ -5.71028893  14.71028893]
Eigen Vectors:
[[-0.9259401  -0.6894021 ]
 [ 0.37767039 -0.72437887]]<jupyter_text>Question 4  Part C<jupyter_code>B = [[-0.75,-1.5,0.5],[3.25,2.5,0.5],[1.25,-2.5,-3.5],[-3.75,1.5,2.5]]
covar = np.cov(B,rowvar = False)
print("Co-variance Matrix for B:")
print(covar)
w,v = np.linalg.eig(covar)
print("Largest Eigen Value:",max(w))<jupyter_output>Largest Eigen Value: 12.9788188701<jupyter_text>Part D<jupyter_code>X = B
import sklearn.decomposition as skd
import numpy as np
pca = skd.PCA(n_components = 3)
skd.PCA.fit(pca,X)
W1 = pca.components_
W = W1.transpose()
Z = pca.transform(X)

print("First 2 Columns:")
for i in range(len(Z)):
    print(Z[i][0:2])<jupyter_output>First 2 Columns:
[ 0.26018674 -1.41900435]
[-0.87353472  4.03721245]
[-4.04749635 -1.8486773 ]
[ 4.66084433 -0.7695308 ]<jupyter_text>Question 5 PCA on images<jupyter_code>import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import fetch_lfw_people
lfw_people = fetch_lfw_people(min_faces_per_person=70)
n_samples, h, w = lfw_people.images.shape
npix = h*w
fea = lfw_people.data
def plt_face(x):
    global h,w
    plt.imshow(x.reshape((h, w)), cmap=plt.cm.gray)
    plt.xticks([])
plt.figure(figsize=(10,20))
nplt = 4
for i in range(nplt):
    plt.subplot(1,nplt,i+1)
    plt_face(fea[i])
plt.show()<jupyter_output><empty_output><jupyter_text>A part<jupyter_code>print("Face 4 in Fea")
plt_face(fea[3])
plt.show()<jupyter_output>Face 3 in Fea<jupyter_text>B Part<jupyter_code># Calculating Mean without library
newFace = np.zeros(h*w)
for height in range(h*w):
    pixel = 0
    for face in range(len(fea)):
        pixel += (fea[face])[height]
    pixel /=len(fea) 
    newFace[height] = pixel  
print("Face created after finding mean")    

# plt_face(newFace)
# Calculating Mean with numpy
plt_face(np.mean(fea,axis = 0))
plt.show()<jupyter_output>Face created after finding mean<jupyter_text>C Part<jupyter_code>pca = skd.PCA(n_components = 5)
skd.PCA.fit(pca,fea)
W1 = pca.components_
W = W1.transpose()
Z = pca.transform(fea)
print("Attributes after PCA:", Z[3])<jupyter_output>Attributes after PCA: [ 202.54211426 -261.47738647  418.97369385  -29.39872742   39.787323  ]<jupyter_text>D Part<jupyter_code>M = np.mean(fea,axis = 0)
a = np.dot(Z, W1)
result = a + M
print("Reconstruction with 5 Components")
plt_face(result[3])
plt.show()
pca = skd.PCA(n_components = 50)
skd.PCA.fit(pca,fea)
W1 = pca.components_
W = W1.transpose()
Z = pca.transform(fea)
M = np.mean(fea,axis = 0)
a = np.dot(Z, W1)
result = a + M
print("Reconstruction with 50 components")
plt_face(result[3])
plt.show()<jupyter_output>Reconstruction with 50 components
{'fraction_non_alphanumeric': 0.15636485131974606, 'fraction_numerical': 0.09021049114600735, 'mean_word_length': 5.343220338983051, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '72224', 'license': 'MIT', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1304, 'n_words': 256, 'path': 'PCA/ML_HW3Code.ipynb', 'repo_name': 'ayush159/ML-Algorithms', 'size': '3005'}