1.
import numpy as np
x = np.array([[-1,-1,-1],[-2,-1,-1.5],[-3,-2,-2]])
x
#normalize the data
x_scaled = np.array(list(map(lambda y:(y-np.mean(y))/np.std(y),x.T))).T
x_scaled
#Get the covariance matrix and calculte its eigenvalues and eigenvectors
cov_matrix = np.cov(x_scaled.T)
cov_matrix
#eigenvector,eigenvalue
eig_vals,eig_vecs = np.linalg.eig(cov_matrix)
#Get the top k eigenvectors of the covariance matrix where k is the size of the desired subspace.
eigen_pairs = [(np.abs(eig_vals[i]),eig_vecs[:,i]) for i in range(len(eig_vals))]
eigen_pairs.sort(reverse=True)
for (eig_vals,eig_vecs) in eigen_pairs:
print('Eigen values: {0}\nEigen vectors: {1}'.format(eig_vals,eig_vecs), end='\n\n')
projection_matrix = np.hstack((eigen_pairs[0][1][:,np.newaxis],eigen_pairs[1][1][:,np.newaxis]))
print('Projection Matrix \n\n',projection_matrix)
#project the data into lower subspace
x_pca_manual = np.dot(x_scaled,projection_matrix)
x_pca_manual