• PCA问题汇总


    PCA(主成分分析)通常用于降维和数据可视化,以帮助理解数据的结构和关系。在进行 PCA 分析后,你可以选择按照不同的方式来绘制图表,具体取决于你的分析目的和所关心的信息。
    按类别绘制的三维散点图:

    1.意义: 在这种图表中,每个数据点代表一个样本,而不同类别的样本用不同的颜色或标记来区分。
    2.如何区分: 每个数据点的位置表示它在 PCA 空间中的投影。不同类别的样本可能在 PCA 空间中聚集在一起,也可能分散开来,这可以帮助你观察不同类别之间的相似性和差异性。

    按主成分绘制的三维散点图:

    3.意义: 在这种图表中,每个数据点的位置表示它在不同主成分上的得分,而不是按照原始数据的类别进行分类。
    4.如何区分: 不同主成分方向上的数据点分布情况可以帮助你理解不同主成分的贡献和样本之间的关系。例如,PC1 可能捕捉到数据集中的某种整体趋势,而 PC2 和 PC3 则可能捕捉到更细微的差异。

    选择按类别绘制还是按主成分绘制的图表取决于你感兴趣的信息。如果你想要了解不同类别之间的差异和相似性,那么按类别绘制的三维散点图可能更适合。如果你更关心不同主成分之间的关系以及它们如何解释数据的方差,那么按主成分绘制的图表可能更合适。
    在任何一种情况下,确保图表能够清晰地传达你想要的信息,并采用适当的颜色或标记来区分不同的类别或主成分

    1. %合并每个类别的处理后数据
    2. class_data1 = data3(1:10, :); % 类别1数据
    3. class_data2 = data3(11:20, :); % 类别2数据
    4. class_data3 = data3(21:30, :); % 类别3数据
    5. %主成分分析每个类别的数据
    6. [coeff1, score1, ~, ~, explained1] = pca(class_data1);
    7. [coeff2, score2, ~, ~, explained2] = pca(class_data2);
    8. [coeff3, score3, ~, ~, explained3] = pca(class_data3);
    9. %选择保留的主成分数量(根据解释的方差比例)
    10. desiredExplainedVariance = 95; % 保留95%的解释方差
    11. numComponents1 = find(cumsum(explained1) >= desiredExplainedVariance, 1);
    12. numComponents2 = find(cumsum(explained2) >= desiredExplainedVariance, 1);
    13. numComponents3 = find(cumsum(explained3) >= desiredExplainedVariance, 1);
    14. %重新构造数据,仅保留所选的主成分
    15. reconstructedData1 = score1(:, 1:numComponents1) * coeff1(:, 1:numComponents1)';
    16. reconstructedData2 = score2(:, 1:numComponents2) * coeff2(:, 1:numComponents2)';
    17. reconstructedData3 = score3(:, 1:numComponents3) * coeff3(:, 1:numComponents3)';
    18. % 绘制解释方差比例图
    19. figure;
    20. subplot(3,1,1);
    21. pareto(explained1);
    22. title('PANC-NC-1 解释方差比例图');
    23. subplot(3,1,2);
    24. pareto(explained2);
    25. title('PANC-NC-2 解释方差比例图');
    26. subplot(3,1,3);
    27. pareto(explained3);
    28. title('PANC1 解释方差比例图');
    29. % 绘制PCA二维散点图
    30. figure;
    31. subplot(3,1,1);
    32. scatter(score1(:,1), score1(:,2), 'g');
    33. xlabel('Principal Component 1');
    34. ylabel('Principal Component 2');
    35. title('PANC-NC-1 PCA二维散点图');
    36. subplot(3,1,2);
    37. scatter(score2(:,1), score2(:,2), 'r');
    38. xlabel('Principal Component 1');
    39. ylabel('Principal Component 2');
    40. title('PANC-NC-2 PCA二维散点图');
    41. subplot(3,1,3);
    42. scatter(score3(:,1), score3(:,2), 'b');
    43. xlabel('Principal Component 1');
    44. ylabel('Principal Component 2');
    45. title('PANC1 PCA二维散点图');
    46. % 绘制PCA三维散点图
    47. figure;
    48. subplot(3,1,1);
    49. scatter3(score1(:,1), score1(:,2), score1(:,3), 'g');
    50. xlabel('Principal Component 1');
    51. ylabel('Principal Component 2');
    52. zlabel('Principal Component 3');
    53. title('PANC-NC-1 PCA三维散点图');
    54. subplot(3,1,2);
    55. scatter3(score2(:,1), score2(:,2), score2(:,3), 'r');
    56. xlabel('Principal Component 1');
    57. ylabel('Principal Component 2');
    58. zlabel('Principal Component 3');
    59. title('PANC-NC-2 PCA三维散点图');
    60. subplot(3,1,3);
    61. scatter3(score3(:,1), score3(:,2), score3(:,3), 'b');
    62. xlabel('Principal Component 1');
    63. ylabel('Principal Component 2');
    64. zlabel('Principal Component 3');
    65. title('PANC1 PCA三维散点图');
    66. %PANC-NC-1 解释方差比例图
    67. figure;
    68. pareto(explained1);
    69. title('PANC-NC-1 解释方差比例图');
    70. % PANC-NC-1 PCA二维散点图
    71. figure;
    72. scatter(score1(:,1), score1(:,2), 'g');
    73. xlabel('Principal Component 1');
    74. ylabel('Principal Component 2');
    75. title('PANC-NC-1 PCA二维散点图');
    76. % PANC-NC-2 解释方差比例图
    77. figure;
    78. pareto(explained2);
    79. title('PANC-NC-2 解释方差比例图');
    80. % PANC-NC-2 PCA二维散点图
    81. figure;
    82. scatter(score2(:,1), score2(:,2), 'r');
    83. xlabel('Principal Component 1');
    84. ylabel('Principal Component 2');
    85. title('PANC-NC-2 PCA二维散点图');
    86. % PANC1 解释方差比例图
    87. figure;
    88. pareto(explained3);
    89. title('PANC1 解释方差比例图');
    90. % PANC1 PCA二维散点图
    91. figure;
    92. scatter(score3(:,1), score3(:,2), 'b');
    93. xlabel('Principal Component 1');
    94. ylabel('Principal Component 2');
    95. title('PANC1 PCA二维散点图');
    96. % PANC-NC-1 PCA三维散点图
    97. figure;
    98. scatter3(score1(:,1), score1(:,2), score1(:,3), 'g');
    99. xlabel('Principal Component 1');
    100. ylabel('Principal Component 2');
    101. zlabel('Principal Component 3');
    102. title('PANC-NC-1 PCA三维散点图');
    103. % PANC-NC-2 PCA三维散点图
    104. figure;
    105. scatter3(score2(:,1), score2(:,2), score2(:,3), 'r');
    106. xlabel('Principal Component 1');
    107. ylabel('Principal Component 2');
    108. zlabel('Principal Component 3');
    109. title('PANC-NC-2 PCA三维散点图');
    110. % PANC1 PCA三维散点图
    111. figure;
    112. scatter3(score3(:,1), score3(:,2), score3(:,3), 'b');
    113. xlabel('Principal Component 1');
    114. ylabel('Principal Component 2');
    115. zlabel('Principal Component 3');
    116. title('PANC1 PCA三维散点图');
    117. % 绘制修改后的三维散点图
    118. figure;
    119. % 绘制 PANC-NC-1 的数据点
    120. scatter3(score1(:,1), score1(:,2), score1(:,3), 50, 'g', 'filled', 'DisplayName', 'PANC-NC-1');
    121. hold on;
    122. % 绘制 PANC-NC-2 的数据点
    123. scatter3(score2(:,1), score2(:,2), score2(:,3), 50, 'r', 'filled', 'DisplayName', 'PANC-NC-2');
    124. % 绘制 PANC1 的数据点
    125. scatter3(score3(:,1), score3(:,2), score3(:,3), 50, 'b', 'filled', 'DisplayName', 'PANC1');
    126. % 添加主成分得分
    127. text(score1(:,1), score1(:,2), score1(:,3), num2str((1:numel(score1(:,1)))'), 'VerticalAlignment','bottom', 'HorizontalAlignment','right');
    128. text(score2(:,1), score2(:,2), score2(:,3), num2str((1:numel(score2(:,1)))'), 'VerticalAlignment','bottom', 'HorizontalAlignment','right');
    129. text(score3(:,1), score3(:,2), score3(:,3), num2str((1:numel(score3(:,1)))'), 'VerticalAlignment','bottom', 'HorizontalAlignment','right');
    130. xlabel('Principal Component 1');
    131. ylabel('Principal Component 2');
    132. zlabel('Principal Component 3');
    133. title('PCA三维散点图');
    134. legend('Location', 'best');
    135. hold off;

    下面是一个绘制散点图添加置信椭圆的代码ai版

    1. % 假设你已经有了 class_data1、class_data2 和 class_data3
    2. % 假设你已经有了 coeff1、coeff2、coeff3(主成分矩阵)
    3. % 假设你已经有了 score1、score2、score3(主成分得分矩阵)
    4. % 假设你已经有了 numComponents1、numComponents2、numComponents3
    5. % 绘制散点图
    6. figure;
    7. hold on;
    8. scatter3(score1(:,1), score1(:,2), score1(:,3), 50, 'r', 'filled');
    9. scatter3(score2(:,1), score2(:,2), score2(:,3), 50, 'g', 'filled');
    10. scatter3(score3(:,1), score3(:,2), score3(:,3), 50, 'b', 'filled');
    11. xlabel('PC1');
    12. ylabel('PC2');
    13. zlabel('PC3');
    14. title('PCA Scatter Plot');
    15. % 计算并绘制置信椭圆
    16. confidenceLevel = 0.95; % 置信水平
    17. numPoints = 100; % 网格点数
    18. % 对每个类别执行
    19. for i = 1:3
    20. % 提取当前类别的主成分得分
    21. if i == 1
    22. score = score1(:, 1:numComponents1);
    23. elseif i == 2
    24. score = score2(:, 1:numComponents2);
    25. else
    26. score = score3(:, 1:numComponents3);
    27. end
    28. % 拟合置信椭圆
    29. [center, radii, evecs, v] = fit_ellipse(score(:,1), score(:,2), confidenceLevel);
    30. % 生成置信椭圆的参数
    31. theta_grid = linspace(0,2*pi,numPoints);
    32. phi = atan2(evecs(2,2),evecs(1,2));
    33. ellipse_x = radii(1)*cos(theta_grid)*cos(phi) - radii(2)*sin(theta_grid)*sin(phi) + center(1);
    34. ellipse_y = radii(1)*cos(theta_grid)*sin(phi) + radii(2)*sin(theta_grid)*cos(phi) + center(2);
    35. ellipse_z = zeros(size(ellipse_x)) + center(3); % 由于是三维数据,置信椭圆位于平面上,Z坐标设为常数
    36. % 绘制置信椭圆
    37. plot3(ellipse_x, ellipse_y, ellipse_z, 'k--', 'LineWidth', 2);
    38. end
    39. legend('Class 1', 'Class 2', 'Class 3', 'Confidence Ellipse');
    40. grid on;
    41. hold off;

     存档参考,如需使用,需要大改

  • 相关阅读:
    C#使用TcpListener进行聊天通信
    同步整流 降压恒流 输入4-40V 功率可达40W 电流3.6A 原理图
    【C# Programming】类、构造器、静态成员
    详细指南:如何使用基于Double-Array Trie树的PHP扩展实现垃圾邮件过滤器
    猿创征文|有了这8个开发工具,程序员可以早点下班了
    麒麟系统安装找不到安装源!!!!设置基础软件仓库时出错
    上线服务时遇到的一个SSLHandshakeException错误
    【力扣】整数反转,判断是否溢出的数学解法
    【沐风老师】3DMAX一款神级一键四边面重拓扑插件Quad Remesher使用教程
    ch4-3 音频信号的频域特征
  • 原文地址:https://blog.csdn.net/qq_58732322/article/details/136373862