<jupyter_start><jupyter_text>Exercise 2)<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
def matr(eps):
  A = np.array([[1, 1, 1], [eps, 0, 0],[0, eps, 0],[0, 0, eps]])
  print("Matrix A:\n", A.astype(str))
  C = np.dot(A.T, A)
  print("Covariance matrix:\n", C.astype(str))
  Lambda, _ = np.linalg.eig(C)
  print('Eigen Value:\n', Lambda.astype(str))
  s = np.sqrt(sorted(Lambda, reverse=True))
  print("Singular value via PCA:\n", s.astype(str))
  u, s_, v = np.linalg.svd(A)
  print("Singular value via SVD:\n", s_.astype(str))
eps = 1.0E-7
matr(eps)
eps = 1.0E-8
matr(eps)<jupyter_output>Matrix A:
 [['1.0' '1.0' '1.0']
 ['1e-08' '0.0' '0.0']
 ['0.0' '1e-08' '0.0']
 ['0.0' '0.0' '1e-08']]
Covariance matrix:
 [['1.0' '1.0' '1.0']
 ['1.0' '1.0' '1.0']
 ['1.0' '1.0' '1.0']]
Eigen Value:
 ['-2.220446049250313e-16' '3.0' '0.0']
Singular value via PCA:
 ['1.7320508075688772' '0.0' 'nan']
Singular value via SVD:
 ['1.7320508075688776' '1e-08' '9.999999999999995e-09']<jupyter_text>Question 2.c Problem is one of the eigen value is negative for which there exist no real square root, hence the error. Question 2.d All the diagonal elements of a covariance matrx should be real, however in the above case as we can see,  we got one of the eigen values as negative whose square root is not a real number and thus one wrong result in the singular values calculation via PCA Exercise 3<jupyter_code>import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
file = 'train.csv'
with open(file, 'r') as f:
      data = pd.read_csv(f, delimiter=',',header=None)
labels = data.iloc[:,-1]
data.drop(data.columns[len(data.columns)-1], axis=1, inplace=True)
scaler = StandardScaler()
data_scaled = scaler.fit_transform(data)
pca = PCA()
projections = pca.fit_transform(data_scaled)
projections = projections[:,0:2]
import numpy as np
cumulative = np.cumsum(pca.explained_variance_ratio_)
plt.plot(np.arange(1, len(pca.explained_variance_ratio_)+1), cumulative, marker='o')
#plt.gca().set_aspect('equal', adjustable='box')
plt.xticks(np.arange(1, 19))
plt.title('Explained variance(cumulated)')
plt.xlabel('Eigenvalue $\lambda_i$')
plt.ylabel('%')
plt.grid(True)
plt.draw()
import numpy as np
plt.figure()
ax = plt.axes()
scatter = ax.scatter(projections[:,0], projections[:,1], c=labels)
ax.legend(handles=scatter.legend_elements(num=[1,2,3,4])[0], labels=['1','2','3','4'])
plt.draw()
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
clf = LDA()
proj = clf.fit_transform(data, labels)[:,0:2]
plt.figure()
ax = plt.axes()
scatter = ax.scatter(proj[:,0], proj[:,1], c=labels)
ax.legend(handles=scatter.legend_elements(num=[1,2,3,4])[0], labels=['1','2','3','4'])
plt.draw()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1853448275862069, 'fraction_numerical': 0.06357758620689655, 'mean_word_length': 5.522248243559719, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '612140', 'license': 'Apache-2.0', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1126, 'n_words': 268, 'path': 'PR/Assignment09/PCAvsLDA.ipynb', 'repo_name': 'jhinga-la-la/pattern-recognition-course', 'size': '2784'}