Skip to content

PCA图 (主成分 biplot & 三维图) 绘制与解释 | python

问题:如何绘制二维和三维的主成分图,以及如何解释?

Code: 二维图

import matplotlib.pyplot as plt
import numpy as np

def biplot(reduced_data, labels, pc, variable):
    """plot compositional biplot for two principle components
    
    :param reduced_data: data processed by PCA
    :param labels: labels of original dataset 
    :param pc: all the principle components
    :param variable: the name of the variables of the data set
    """
    plt.figure(1, figsize=(14, 10))
    
    legend = []  # 
    classes = np.unique(labels)  # label type
    n = pc.shape[1]
    # colors = ['g', 'r', 'y']
    # markers = ['o', '^', 'x']
    
    x = reduced_data[:, 0]  # variable contributions for PC1
    y = reduced_data[:, 1]  # variable contributions for PC2
    scalex = 1.0/(x.max() - x.min())
    scaley = 1.0/(y.max() - y.min())
    
    # Draw a data point projection plot that is projected to 
    # a two-dimensional plane using normal PCA
    for i, label in enumerate(classes):
        plt.scatter(x[labels==label] * scalex,
                    y[labels==label] * scaley,
                    linewidth=0.01) 
        # hyperparameter in plt.scatter(): c=colors[i], marker=markers[i]
        legend.append("Label: {}".format(label))
     
    plt.legend(legend)
    
    # plot arrows as the variable contribution,
    # each variable has a score for PC1 and for PC2 respectively
    for i in range(n):
        plt.arrow(0, 0, pc[0, i], pc[1, i], color='k', alpha=0.7,
                  linewidth=1, )
        plt.text(pc[0, i]*1.01, pc[1, i]*1.01, variable[i],
                 ha='center', va='center', color='k', fontsize=12)
        
    plt.xlabel("$PC1$")
    plt.ylabel("$PC2$")
    plt.title("Compositional biplot")
    plt.grid()
    save_fig("Compositional biplot")

在这里插入图片描述
解释

  1. 能够清楚的看到哪些特征对主成分(PC)的贡献值大,一般来说,是分别分析哪些特征对PC1较为重要,哪些特征对PC2较为重要;
  2. 对于PC1来说,元素的横坐标的绝对值越大,越重要
  3. 对于PC2来说,元素的纵坐标的绝对值越大,越重要

Code:三维图

import matplotlib.pyplot as plt
import numpy as np

def componential_plot_static_3d(reduced_data, labels, pc, variable):
    """draw a static componential plot in 3d for three principle components
    
    :param reduced_data: data processed by PCA
    :param labels: labels of original dataset 
    :param pc: all the principle components
    :param variable: the name of the variables of the data set
    """
    
    fig = plt.figure(1, figsize=(14, 12))
    ax = plt.axes(projection='3d')
    
    legend = []  # 
    classes = np.unique(labels)  # label type
    n = pc.shape[1]
    # colors = ['g', 'r', 'y']
    # markers = ['o', '^', 'x']
    
    x = reduced_data[:, 0]  # variable contributions for PC1
    y = reduced_data[:, 1]  # variable contributions for PC2 
    z = reduced_data[:, 2]  # variable contributions for PC3
    scalex = 1.0/(x.max() - x.min())
    scaley = 1.0/(y.max() - y.min())
    scalez = 1.0/(z.max() - z.min())
    
    # Draw a data point projection plot that is projected to 
    # a three-dimensional space using normal PCA
    for i, label in enumerate(classes):
        ax.scatter3D(x[labels==label] * scalex,
                     y[labels==label] * scaley,
                     z[labels==label] * scalez,
                     linewidth=0.01) 
        # hyperparameter in plt.scatter(): c=colors[i], marker=markers[i]
        legend.append("Label: {}".format(label))
    
    ax.legend(legend)
    
    # the initial angle to draw the 3d plot
    azim = -60  # azimuth
    elev = 30   # elevation
    ax.view_init(elev, azim)  # set the angles
    
    # plot arrows as the variable contribution,
    # each variable has a score for PC1, for PC2 and for PC3 respectively
    for i in range(n):
        ax.quiver(0, 0, 0, pc[0, i], pc[1, i], pc[2, i], color='k', alpha=0.7,
                  linewidth=1, arrow_length_ratio=0.05)
        ax.text(pc[0, i]*1.1, pc[1, i]*1.1, pc[2, i]*1.1, variable[i],
                ha='center', va='center', color='k', fontsize=12)
        
    ax.set_xlabel("$PC1$")
    ax.set_ylabel("$PC2$")
    ax.set_zlabel("$PC3$")
    plt.title("Componential Plot in 3 Dimension")
    plt.grid()
    save_fig("Componential_Plot_in_3_Dimension")

在这里插入图片描述

  • elevation 仰角;azimuth方位角,ax.view_init(elev, azim)设置视角
  • 当elevation=0时,视角为沿x1负方向看,当elevation=90时,视角沿x3负方向看。
  • 当azimuth=0时,视角为沿x1负方向看,当azimuth=90时,视角沿x2负方向看。
  • 随着azimuth的增加,从x3负方向看,x1x2平面是顺时针旋转的。
  • 逆时针旋转,能把x1,x2的大小顺序调整为常规平面坐标系。

进一步学习【如何绘制三维PCA动图】

参考资料:
PCA clearly explained —When, Why, How to use it and feature importance: A guide in Python


聚圣源南瓜网明代服饰鉴字起名妊娠是什么意思管理公司的起名奥格瑞拉起名字大师辰东新书《大宇宙》小说浙江少儿频道测试起名1518姓名物品取名起名大全开包子店起名终极觉醒天浴李小璐姓宋给小孩起名字中华起名网免费测名1038万人起名高尔夫网球生活用纸起什么名字好葫芦店起名大全给群起个有趣的名字左姓宝宝起名字越狱软件下载回到未来2快播ram是什么意思ekp守卫剑阁1.29夏日狂情时尚女宝宝起名夏河洛洛分手壮志高飞剧情介绍淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻让美丽中国“从细节出发”清明节放假3天调休1天男孩疑遭霸凌 家长讨说法被踢出群国产伟哥去年销售近13亿网友建议重庆地铁不准乘客携带菜筐雅江山火三名扑火人员牺牲系谣言代拍被何赛飞拿着魔杖追着打月嫂回应掌掴婴儿是在赶虫子山西高速一大巴发生事故 已致13死高中生被打伤下体休学 邯郸通报李梦为奥运任务婉拒WNBA邀请19岁小伙救下5人后溺亡 多方发声王树国3次鞠躬告别西交大师生单亲妈妈陷入热恋 14岁儿子报警315晚会后胖东来又人满为患了倪萍分享减重40斤方法王楚钦登顶三项第一今日春分两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?周杰伦一审败诉网易房客欠租失踪 房东直发愁男子持台球杆殴打2名女店员被抓男子被猫抓伤后确诊“猫抓病”“重生之我在北大当嫡校长”槽头肉企业被曝光前生意红火男孩8年未见母亲被告知被遗忘恒大被罚41.75亿到底怎么缴网友洛杉矶偶遇贾玲杨倩无缘巴黎奥运张立群任西安交通大学校长黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发妈妈回应孩子在校撞护栏坠楼考生莫言也上北大硕士复试名单了韩国首次吊销离岗医生执照奥巴马现身唐宁街 黑色着装引猜测沈阳一轿车冲入人行道致3死2伤阿根廷将发行1万与2万面值的纸币外国人感慨凌晨的中国很安全男子被流浪猫绊倒 投喂者赔24万手机成瘾是影响睡眠质量重要因素春分“立蛋”成功率更高?胖东来员工每周单休无小长假“开封王婆”爆火:促成四五十对专家建议不必谈骨泥色变浙江一高校内汽车冲撞行人 多人受伤许家印被限制高消费

聚圣源 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化