栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pca实例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pca实例

import numpy as np
import math

#求均值
x = [2.5 , 0.5 , 2.2 , 1.9 , 3.1 , 2.3 , 2.0 , 1.0 , 1.5 , 1.1]
x_mean = np.mean(x)
y = [2.4 , 0.7 , 2.9 , 2.2 , 3.0 , 2.7 , 1.6 , 1.1 , 1.6 , 0.9]
y_mean = np.mean(y)

#求dataadjust数值
xx = np.array([2.5 , 0.5 , 2.2 , 1.9 , 3.1 , 2.3 , 2.0 , 1.0 , 1.5 , 1.1])
yy = np.array([2.4 , 0.7 , 2.9 , 2.2 , 3.0 , 2.7 , 1.6 , 1.1 , 1.6 , 0.9])
dataadjust_xx = np.linspace(-2,2,10)
dataadjust_yy = np.linspace(-2,2,10)
for n in range(0,10):
    dataadjust_xx[n] = xx[n] - x_mean
    dataadjust_yy[n] = yy[n] - y_mean
for n in range(10): 
    print('%-30s%-20s' %(dataadjust_xx[n],dataadjust_yy[n]))
    
#求协方差
xi = np.array([0.61 , -1.31 , 0.39 , 0.09 , 1.29 , 0.49 ,
               0.19 , -0.81 , -0.31 , -0.71])
yi = np.array([0.49 ,-1.21,0.99,0.29,
              1.09,0.79,-0.31,-0.81,-0.31,-1.01])
covxy = np.cov(xi,yi)
print(covxy)

#求特征向量和特征值
a,b = np.linalg.eig(covxy)
print('特征值:{}n特征向量:{}'.format(a,b))

#求特征向量的最大值
max_a = max(a)
print(max_a)
max_b = np.array([-0.7385647 ,-0.6741826])
bt = max_b.reshape(max_b.shape[0],1)
print(bt)

#求fd
dataadjust_xy = np.array([[2.5 , 0.5 , 2.2 , 1.9 , 3.1 , 2.3 , 2.0 , 1.0 , 1.5 , 1.1],
                        [2.4 , 0.7 , 2.9 , 2.2 , 3.0 , 2.7 , 1.6 , 1.1 , 1.6 , 0.9]])
fd = dataadjust_xy * bt
print(fd)
0.69                          0.48999999999999977 
-1.31                         -1.2100000000000002 
0.3900000000000001            0.9899999999999998  
0.08999999999999986           0.29000000000000004 
1.29                          1.0899999999999999  
0.48999999999999977           0.79                
0.18999999999999995           -0.31000000000000005
-0.81                         -0.81               
-0.31000000000000005          -0.31000000000000005
-0.71                         -1.0100000000000002 
[[0.60492889 0.61108889]
 [0.61108889 0.71655556]]
特征值:[0.04710979 1.27437465]
特征向量:[[-0.7385647 -0.6741826]
 [ 0.6741826 -0.7385647]]
1.2743746515325012
[[-0.7385647]
 [-0.6741826]]
[[-1.84641175 -0.36928235 -1.62484234 -1.40327293 -2.28955057 -1.69869881
  -1.4771294  -0.7385647  -1.10784705 -0.81242117]
 [-1.61803824 -0.47192782 -1.95512954 -1.48320172 -2.0225478  -1.82029302
  -1.07869216 -0.74160086 -1.07869216 -0.60676434]]
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/300559.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号