pca learning #5

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值