pca算法介绍及java实现_PCA算法数学原理及实现

from numpy import *import numpyasnp

import matplotlib.pyplotaspltfromscipy.io import loadmat

cx= mat([[2.5, 2.4],

[0.5, 0.7],

[2.2, 2.9],

[1.9, 2.2],

[3.1, 3.0],

[2.3, 2.7],

[2, 1.6],

[1, 1.1],

[1.5, 1.6],

[1.1, 0.9]])

# print(cx.shape)

sz=cx.shape

m= sz[0]

n= sz[1]

# 显示原数据

def plot_oridata( cx ):

plt.figure(num='原数据图', figsize=(6, 6))

plt.xlabel('Feature 1')

plt.ylabel('Feature 2')

plt.xlim((-1, 4))

plt.ylim((-1, 4))

new_ticks= np.arange(-1, 4, 0.5)

plt.xticks(new_ticks)

plt.yticks(new_ticks)

plt.scatter(cx[:,0].tolist(), cx[:, 1].tolist(), c='r', marker='+')

plt.plot([0, 0], [-1, 4], 'k-')

plt.plot([-1, 4], [0, 0], 'k-')

plt.show()return#求协方差矩阵

def get_covMat( cx ):

print('+++++++++++++ 求协方差矩阵 +++++++++++++++')

# 零均值化

ecol= np.mean(cx, axis=0)

cx1= (cx[:, 0]) - ecol[0, 0]

cx2= cx[:, 1] - ecol[0, 1]

Mcx=np.column_stack((cx1, cx2))

Covx= np.transpose(Mcx)*Mcx/(m-1)

# print(Covx)returnCovx, Mcx

#计算特征值和特征向量

def get_eign(Covx, k):

eVals, eVecs=np.linalg.eig(Covx)

# print(eVals)

# print(eVecs,' ', eVecs.shape)

sorted_indices=np.argsort(eVals)

topk_evecs= eVecs[:, sorted_indices[:-k-1:-1]]

# print(topk_evecs)

plt.figure(num='特征向量', figsize=(6, 6))

plt.xlabel('Feature 1')

plt.ylabel('Feature 2')

plt.xlim((-4, 4))

plt.ylim((-4, 4))

new_ticks= np.arange(-4, 4, 1)

plt.xticks(new_ticks)

plt.yticks(new_ticks)

plt.scatter(cx[:,0].tolist(), cx[:, 1].tolist(), c='r', marker='+')

plt.plot([0, 0], [-4, 4], 'k-.')

plt.plot([-4, 4], [0, 0], 'k-.')

# print(eVecs[0, 0], eVecs[1, 0])

# print(eVecs)

plt.plot([0, eVecs[0, 0] * 6], [0, eVecs[1, 0] * 6], 'b:')

plt.plot([0, eVecs[0, 1] * 6], [0, eVecs[1, 1] * 6], 'b:')

plt.plot([0, eVecs[0, 0] * -6], [0, eVecs[1, 0] * -6], 'b:')

plt.plot([0, eVecs[0, 1] * -6], [0, eVecs[1, 1] * -6], 'b:')

plt.show()returneVecs, topk_evecs

#转换数据

def transform_data(eVecs, Mcx):

print("------------------转换数据---------------------")

tran_data= Mcx *eVecs

plt.figure(num='转换数据', figsize=(6, 6))

plt.xlabel('Feature 1')

plt.ylabel('Feature 2')

plt.xlim((-2, 2))

plt.ylim((-2, 2))

new_ticks= np.arange(-2, 2, 0.5)

plt.xticks(new_ticks)

plt.yticks(new_ticks)

plt.scatter(tran_data[:,0].tolist(), tran_data[:, 1].tolist(), c='r', marker='+')#哪一维对应x,哪一维对应y

plt.plot([0, 0], [-4, 4], 'k-.')

plt.plot([-4, 4], [0, 0], 'k-.')

# print(eVecs[0, 0], eVecs[1, 0])

# print(eVecs)

plt.show()return#压缩数据

def compress_data(Mcx, topkevecs, eVecs):

print("------------------压缩数据---------------------")

comdata= Mcx *topkevecs

c1= np.zeros((10, 1), dtype=int)

comdata1=np.column_stack((c1, comdata))

comdata2= comdata1 *eVecs

plt.figure(num='压缩数据', figsize=(6, 6))

plt.xlabel('Feature 1')

plt.ylabel('Feature 2')

plt.xlim((-4, 4))

plt.ylim((-4, 4))

new_ticks= np.arange(-4, 4, 0.5)

plt.xticks(new_ticks)

plt.yticks(new_ticks)

plt.scatter(comdata2[:,0].tolist(), comdata2[:, 1].tolist(), c='r', marker='+') # 哪一维对应x,哪一维对应y

plt.plot([0, 0], [-4, 4], 'k-.')

plt.plot([-4, 4], [0, 0], 'k-.')

plt.plot([0, eVecs[0, 0] * 6], [0, eVecs[1, 0] * 6], 'b:')

plt.plot([0, eVecs[0, 1] * 6], [0, eVecs[1, 1] * 6], 'b:')

plt.plot([0, eVecs[0, 0] * -6], [0, eVecs[1, 0] * -6], 'b:')

plt.plot([0, eVecs[0, 1] * -6], [0, eVecs[1, 1] * -6], 'b:')

# print(eVecs[0, 0], eVecs[1, 0])

# print(eVecs)

plt.show()returnplot_oridata(cx)

Covx, Mcx=get_covMat(cx)

eVecs, topk_evecs= get_eign(Covx, 1)

transform_data(eVecs, Mcx)

compress_data(Mcx, topk_evecs, eVecs)

print('end')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值