云计算百科
云计算领域专业知识百科平台

机器学习第十七讲:PCA → 把100维数据压缩成3D视图仍保持主要特征

机器学习第十七讲:PCA → 把100维数据压缩成3D视图仍保持主要特征

资料取自《零基础学机器学习》。 查看总目录:学习大纲

关于DeepSeek本地部署指南可以看下我之前写的文章:DeepSeek R1本地与线上满血版部署:超详细手把手指南


主成分分析(PCA)是一种数据降维魔法师,能够在保留关键信息的前提下,把复杂数据"折叠"成更简单形式。就像用3句话总结一本小说核心情节[^5-4]。下面通过超市顾客数据案例讲解:

一、核心思想(如同快递装箱)

假设要运输100件形状各异的货物(即100维特征),但只能用3个标箱打包:

#mermaid-svg-Y8OnoZLmBQHdD2Ac {font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .error-icon{fill:#552222;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .marker.cross{stroke:#333333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .cluster-label text{fill:#333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .cluster-label span{color:#333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .label text,#mermaid-svg-Y8OnoZLmBQHdD2Ac span{fill:#333;color:#333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .node rect,#mermaid-svg-Y8OnoZLmBQHdD2Ac .node circle,#mermaid-svg-Y8OnoZLmBQHdD2Ac .node ellipse,#mermaid-svg-Y8OnoZLmBQHdD2Ac .node polygon,#mermaid-svg-Y8OnoZLmBQHdD2Ac .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .node .label{text-align:center;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .node.clickable{cursor:pointer;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .arrowheadPath{fill:#333333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .cluster text{fill:#333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac .cluster span{color:#333;}#mermaid-svg-Y8OnoZLmBQHdD2Ac div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Y8OnoZLmBQHdD2Ac :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

杂乱货物

找出承载量最大的箱子

按重要性装箱

  • 找主运输箱:选取能装最多货物的箱子(主成分1)
  • 补充空隙:选与第一箱垂直的第二箱装剩余重要货物(主成分2)
  • 反复叠加:直到装满3个箱子(主成分3),丢弃不重要的零碎物品[^5-4]
  • 类比说明:就像用"年收入+消费频率+健康指数"3个维度,就能代表原始30个消费行为特征的核心信息

    二、实战步骤(用超市数据演示)

    案例背景:超市有200个顾客的100维消费数据,包括:

    • 生鲜购买频率
    • 电子产品消费额
    • 促销敏感度
    • …(共100项指标)[^5-4]

    #mermaid-svg-bJKyaZxKF89iiwcC {font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-bJKyaZxKF89iiwcC .error-icon{fill:#552222;}#mermaid-svg-bJKyaZxKF89iiwcC .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-bJKyaZxKF89iiwcC .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-bJKyaZxKF89iiwcC .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-bJKyaZxKF89iiwcC .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-bJKyaZxKF89iiwcC .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-bJKyaZxKF89iiwcC .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-bJKyaZxKF89iiwcC .marker{fill:#333333;stroke:#333333;}#mermaid-svg-bJKyaZxKF89iiwcC .marker.cross{stroke:#333333;}#mermaid-svg-bJKyaZxKF89iiwcC svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-bJKyaZxKF89iiwcC .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-bJKyaZxKF89iiwcC .cluster-label text{fill:#333;}#mermaid-svg-bJKyaZxKF89iiwcC .cluster-label span{color:#333;}#mermaid-svg-bJKyaZxKF89iiwcC .label text,#mermaid-svg-bJKyaZxKF89iiwcC span{fill:#333;color:#333;}#mermaid-svg-bJKyaZxKF89iiwcC .node rect,#mermaid-svg-bJKyaZxKF89iiwcC .node circle,#mermaid-svg-bJKyaZxKF89iiwcC .node ellipse,#mermaid-svg-bJKyaZxKF89iiwcC .node polygon,#mermaid-svg-bJKyaZxKF89iiwcC .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-bJKyaZxKF89iiwcC .node .label{text-align:center;}#mermaid-svg-bJKyaZxKF89iiwcC .node.clickable{cursor:pointer;}#mermaid-svg-bJKyaZxKF89iiwcC .arrowheadPath{fill:#333333;}#mermaid-svg-bJKyaZxKF89iiwcC .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-bJKyaZxKF89iiwcC .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-bJKyaZxKF89iiwcC .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-bJKyaZxKF89iiwcC .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-bJKyaZxKF89iiwcC .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-bJKyaZxKF89iiwcC .cluster text{fill:#333;}#mermaid-svg-bJKyaZxKF89iiwcC .cluster span{color:#333;}#mermaid-svg-bJKyaZxKF89iiwcC div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-bJKyaZxKF89iiwcC :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    原始100维数据

    标准化处理

    计算协方差矩阵

    找出方差最大的方向

    提取前3个主成分

  • 特征标准化:消除量纲差异,让"购买次数"和"消费金额"可比(如同统一用公分和公斤替代市尺和市斤)[^4-1]

  • 寻找主方向:

    • 第一主成分:解释最大方差的方向(如综合消费能力轴)
    • 第二主成分:解释剩余方差且与第一正交(如消费偏好方向)
    • 第三主成分:继续捕捉剩余重要信息(如促销敏感度)[^5-4]
  • 数据投影: 原始100维数据投影到3个主轴上,得到:

    # 转换后的数据示例
    顾客A: [2.5, 0.8, 1.2]
    顾客B: [1.3, 0.7, 0.3]

    数字代表在三个主方向上的强度值

  • 三、神奇效果(三维可视化)

    压缩后的3D散点图能清晰展示客户分群:

    #mermaid-svg-odtCKWJLHcOptJZJ {font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-odtCKWJLHcOptJZJ .error-icon{fill:#552222;}#mermaid-svg-odtCKWJLHcOptJZJ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-odtCKWJLHcOptJZJ .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-odtCKWJLHcOptJZJ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-odtCKWJLHcOptJZJ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-odtCKWJLHcOptJZJ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-odtCKWJLHcOptJZJ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-odtCKWJLHcOptJZJ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-odtCKWJLHcOptJZJ .marker.cross{stroke:#333333;}#mermaid-svg-odtCKWJLHcOptJZJ svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-odtCKWJLHcOptJZJ .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-odtCKWJLHcOptJZJ .cluster-label text{fill:#333;}#mermaid-svg-odtCKWJLHcOptJZJ .cluster-label span{color:#333;}#mermaid-svg-odtCKWJLHcOptJZJ .label text,#mermaid-svg-odtCKWJLHcOptJZJ span{fill:#333;color:#333;}#mermaid-svg-odtCKWJLHcOptJZJ .node rect,#mermaid-svg-odtCKWJLHcOptJZJ .node circle,#mermaid-svg-odtCKWJLHcOptJZJ .node ellipse,#mermaid-svg-odtCKWJLHcOptJZJ .node polygon,#mermaid-svg-odtCKWJLHcOptJZJ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-odtCKWJLHcOptJZJ .node .label{text-align:center;}#mermaid-svg-odtCKWJLHcOptJZJ .node.clickable{cursor:pointer;}#mermaid-svg-odtCKWJLHcOptJZJ .arrowheadPath{fill:#333333;}#mermaid-svg-odtCKWJLHcOptJZJ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-odtCKWJLHcOptJZJ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-odtCKWJLHcOptJZJ .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-odtCKWJLHcOptJZJ .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-odtCKWJLHcOptJZJ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-odtCKWJLHcOptJZJ .cluster text{fill:#333;}#mermaid-svg-odtCKWJLHcOptJZJ .cluster span{color:#333;}#mermaid-svg-odtCKWJLHcOptJZJ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-odtCKWJLHcOptJZJ :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    X轴

    综合消费能力

    Y轴

    品质偏好度

    Z轴

    价格敏感度

    • 簇1(右上角):高消费+爱品质+不差钱 → 重点维护客户
    • 簇2(中部):中等消费+促销敏感 → 精准营销目标
    • 簇3(左下方):低频消费+低单价 → 潜在流失客户[^5-4]

    生活场景:就像用身高/体重/体脂率3个指标代替50项体检数据,医生仍能判断健康状况[^5-4]

    四、应用场景与注意事项

    优势场景:

    • 高维数据可视化(如基因数据)
    • 去除冗余特征(如相片去噪)
    • 加速模型训练(减少计算量)[^5-4]

    使用注意:

    • 主成分无实际含义(类似加密坐标)
    • 会丢失部分细节信息
    • 需要先标准化处理[^4-1]

    目录:总目录 上篇文章:机器学习第十六讲:K-means → 自动把超市顾客分成不同消费群体 下篇文章:机器学习第十八讲:混淆矩阵 → 诊断模型在医疗检查中的误诊情况


    [^4-1]《零基础学机器学习》第四章第二节特征标准化 [^5-4]《零基础学机器学习》第五章第六节降维技术

    赞(0)
    未经允许不得转载:网硕互联帮助中心 » 机器学习第十七讲:PCA → 把100维数据压缩成3D视图仍保持主要特征
    分享到: 更多 (0)

    评论 抢沙发

    评论前必须登录!