zoukankan      html  css  js  c++  java
  • PCA主成分分析的简单示例-opencv、python

    pca:principal component analysis,常见的降维技术

    生成一组多元正态分布的数据,两个随机分布的协方差矩阵:cov(x,x)=5 cov(x,y)=5 cov(y,y)=5 cov(y,x)=25

    import numpy as np
    import matplotlib.pyplot as plt
    
    mean = [20, 20]
    cov = [[5, 5], [5, 25]]
    x, y = np.random.multivariate_normal(mean, cov, 500).T
    
    plt.plot(x, y, '.')
    plt.axis([0, 40, 0, 40])
    plt.xlabel('feature 1')
    plt.ylabel('feature 2')
    
    plt.show()
    

    展示出两个特征向量,一个是数据分布最大方向,也称第一主成分,另一个是方差方向,第二主成分。

    import numpy as np
    import matplotlib.pyplot as plt
    import cv2
    
    mean = [20, 20]
    cov = [[5, 5], [5, 25]]
    X = np.random.multivariate_normal(mean, cov, 500)
    x, y = X.T
    
    mu, eig = cv2.PCACompute(X, np.array([]))
    
    plt.plot(x, y, '.', zorder=1)
    plt.quiver(mean[0], mean[1], eig[0, 0], eig[0, 1],zorder=3, scale=0.2, units='xy')
    plt.quiver(mean[0], mean[1], eig[1, 0], eig[1, 1],zorder=3, scale=0.2, units='xy')
    plt.axis([0, 40, 0, 40])
    plt.xlabel('feature 1')
    plt.ylabel('feature 2')
    plt.show()
    

    利用opencv的PCAProject来旋转数据

    import numpy as np
    import matplotlib.pyplot as plt
    import cv2
    
    mean = [20, 20]
    cov = [[5, 5], [5, 25]]
    X = np.random.multivariate_normal(mean, cov, 500)
    x, y = X.T
    
    mu, eig = cv2.PCACompute(X, np.array([]))
    X2 = cv2.PCAProject(X,mu,eig)
    
    # plt.plot(x, y, '.', zorder=1)
    # plt.quiver(mean[0], mean[1], eig[0, 0], eig[0, 1],zorder=3, scale=0.2, units='xy')
    # plt.quiver(mean[0], mean[1], eig[1, 0], eig[1, 1],zorder=3, scale=0.2, units='xy')
    plt.plot(X2[:,0],X2[:,1],'.')
    plt.axis([-20, 20, -20, 20])
    plt.xlabel('feature 1')
    plt.ylabel('feature 2')
    plt.show()
    
    

    当人,箭头方向是还是原来的(⊙ˍ⊙)

  • 相关阅读:
    Postgresql HStore 插件试用小结
    postgres-xl 安装与部署 【异常处理】ERROR: could not open file (null)/STDIN_***_0 for write, No such file or directory
    GPDB 5.x PSQL Quick Reference
    postgresql 数据库schema 复制
    hive 打印日志
    gp与 pg 查询进程
    jquery table 发送两次请求 解惑
    python 字符串拼接效率打脸帖
    postgresql 日期类型处理实践
    IBM Rational Rose软件下载以及全破解方法
  • 原文地址:https://www.cnblogs.com/foxer-z/p/14481757.html
Copyright © 2011-2022 走看看