PCA(主成分分析)通常用于降维和数据可视化,以帮助理解数据的结构和关系。在进行 PCA 分析后,你可以选择按照不同的方式来绘制图表,具体取决于你的分析目的和所关心的信息。
按类别绘制的三维散点图:
1.意义: 在这种图表中,每个数据点代表一个样本,而不同类别的样本用不同的颜色或标记来区分。
2.如何区分: 每个数据点的位置表示它在 PCA 空间中的投影。不同类别的样本可能在 PCA 空间中聚集在一起,也可能分散开来,这可以帮助你观察不同类别之间的相似性和差异性。
按主成分绘制的三维散点图:
3.意义: 在这种图表中,每个数据点的位置表示它在不同主成分上的得分,而不是按照原始数据的类别进行分类。
4.如何区分: 不同主成分方向上的数据点分布情况可以帮助你理解不同主成分的贡献和样本之间的关系。例如,PC1 可能捕捉到数据集中的某种整体趋势,而 PC2 和 PC3 则可能捕捉到更细微的差异。
选择按类别绘制还是按主成分绘制的图表取决于你感兴趣的信息。如果你想要了解不同类别之间的差异和相似性,那么按类别绘制的三维散点图可能更适合。如果你更关心不同主成分之间的关系以及它们如何解释数据的方差,那么按主成分绘制的图表可能更合适。
在任何一种情况下,确保图表能够清晰地传达你想要的信息,并采用适当的颜色或标记来区分不同的类别或主成分
%合并每个类别的处理后数据
class_data1 = data3(1:10, :); % 类别1数据
class_data2 = data3(11:20, :); % 类别2数据
class_data3 = data3(21:30, :); % 类别3数据
%主成分分析每个类别的数据
[coeff1, score1, ~, ~, explained1] = pca(class_data1);
[coeff2, score2, ~, ~, explained2] = pca(class_data2);
[coeff3, score3, ~, ~, explained3] = pca(class_data3);
%选择保留的主成分数量(根据解释的方差比例)
desiredExplainedVariance = 95; % 保留95%的解释方差
numComponents1 = find(cumsum(explained1) >= desiredExplainedVariance, 1);
numComponents2 = find(cumsum(explained2) >= desiredExplainedVariance, 1);
numComponents3 = find(cumsum(explained3) >= desiredExplainedVariance, 1);
%重新构造数据,仅保留所选的主成分
reconstructedData1 = score1(:, 1:numComponents1) * coeff1(:, 1:numComponents1)';
reconstructedData2 = score2(:, 1:numComponents2) * coeff2(:, 1:numComponents2)';
reconstructedData3 = score3(:, 1:numComponents3) * coeff3(:, 1:numComponents3)';
% 绘制解释方差比例图
figure;
subplot(3,1,1);
pareto(explained1);
title('PANC-NC-1 解释方差比例图');
subplot(3,1,2);
pareto(explained2);
title('PANC-NC-2 解释方差比例图');
subplot(3,1,3);
pareto(explained3);
title('PANC1 解释方差比例图');
% 绘制PCA二维散点图
figure;
subplot(3,1,1);
scatter(score1(:,1), score1(:,2), 'g');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
title('PANC-NC-1 PCA二维散点图');
subplot(3,1,2);
scatter(score2(:,1), score2(:,2), 'r');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
title('PANC-NC-2 PCA二维散点图');
subplot(3,1,3);
scatter(score3(:,1), score3(:,2), 'b');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
title('PANC1 PCA二维散点图');
% 绘制PCA三维散点图
figure;
subplot(3,1,1);
scatter3(score1(:,1), score1(:,2), score1(:,3), 'g');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
zlabel('Principal Component 3');
title('PANC-NC-1 PCA三维散点图');
subplot(3,1,2);
scatter3(score2(:,1), score2(:,2), score2(:,3), 'r');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
zlabel('Principal Component 3');
title('PANC-NC-2 PCA三维散点图');
subplot(3,1,3);
scatter3(score3(:,1), score3(:,2), score3(:,3), 'b');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
zlabel('Principal Component 3');
title('PANC1 PCA三维散点图');
%PANC-NC-1 解释方差比例图
figure;
pareto(explained1);
title('PANC-NC-1 解释方差比例图');
% PANC-NC-1 PCA二维散点图
figure;
scatter(score1(:,1), score1(:,2), 'g');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
title('PANC-NC-1 PCA二维散点图');
% PANC-NC-2 解释方差比例图
figure;
pareto(explained2);
title('PANC-NC-2 解释方差比例图');
% PANC-NC-2 PCA二维散点图
figure;
scatter(score2(:,1), score2(:,2), 'r');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
title('PANC-NC-2 PCA二维散点图');
% PANC1 解释方差比例图
figure;
pareto(explained3);
title('PANC1 解释方差比例图');
% PANC1 PCA二维散点图
figure;
scatter(score3(:,1), score3(:,2), 'b');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
title('PANC1 PCA二维散点图');
% PANC-NC-1 PCA三维散点图
figure;
scatter3(score1(:,1), score1(:,2), score1(:,3), 'g');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
zlabel('Principal Component 3');
title('PANC-NC-1 PCA三维散点图');
% PANC-NC-2 PCA三维散点图
figure;
scatter3(score2(:,1), score2(:,2), score2(:,3), 'r');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
zlabel('Principal Component 3');
title('PANC-NC-2 PCA三维散点图');
% PANC1 PCA三维散点图
figure;
scatter3(score3(:,1), score3(:,2), score3(:,3), 'b');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
zlabel('Principal Component 3');
title('PANC1 PCA三维散点图');
% 绘制修改后的三维散点图
figure;
% 绘制 PANC-NC-1 的数据点
scatter3(score1(:,1), score1(:,2), score1(:,3), 50, 'g', 'filled', 'DisplayName', 'PANC-NC-1');
hold on;
% 绘制 PANC-NC-2 的数据点
scatter3(score2(:,1), score2(:,2), score2(:,3), 50, 'r', 'filled', 'DisplayName', 'PANC-NC-2');
% 绘制 PANC1 的数据点
scatter3(score3(:,1), score3(:,2), score3(:,3), 50, 'b', 'filled', 'DisplayName', 'PANC1');
% 添加主成分得分
text(score1(:,1), score1(:,2), score1(:,3), num2str((1:numel(score1(:,1)))'), 'VerticalAlignment','bottom', 'HorizontalAlignment','right');
text(score2(:,1), score2(:,2), score2(:,3), num2str((1:numel(score2(:,1)))'), 'VerticalAlignment','bottom', 'HorizontalAlignment','right');
text(score3(:,1), score3(:,2), score3(:,3), num2str((1:numel(score3(:,1)))'), 'VerticalAlignment','bottom', 'HorizontalAlignment','right');
xlabel('Principal Component 1');
ylabel('Principal Component 2');
zlabel('Principal Component 3');
title('PCA三维散点图');
legend('Location', 'best');
hold off;
下面是一个绘制散点图添加置信椭圆的代码ai版
% 假设你已经有了 class_data1、class_data2 和 class_data3
% 假设你已经有了 coeff1、coeff2、coeff3(主成分矩阵)
% 假设你已经有了 score1、score2、score3(主成分得分矩阵)
% 假设你已经有了 numComponents1、numComponents2、numComponents3
% 绘制散点图
figure;
hold on;
scatter3(score1(:,1), score1(:,2), score1(:,3), 50, 'r', 'filled');
scatter3(score2(:,1), score2(:,2), score2(:,3), 50, 'g', 'filled');
scatter3(score3(:,1), score3(:,2), score3(:,3), 50, 'b', 'filled');
xlabel('PC1');
ylabel('PC2');
zlabel('PC3');
title('PCA Scatter Plot');
% 计算并绘制置信椭圆
confidenceLevel = 0.95; % 置信水平
numPoints = 100; % 网格点数
% 对每个类别执行
for i = 1:3
% 提取当前类别的主成分得分
if i == 1
score = score1(:, 1:numComponents1);
elseif i == 2
score = score2(:, 1:numComponents2);
else
score = score3(:, 1:numComponents3);
end
% 拟合置信椭圆
[center, radii, evecs, v] = fit_ellipse(score(:,1), score(:,2), confidenceLevel);
% 生成置信椭圆的参数
theta_grid = linspace(0,2*pi,numPoints);
phi = atan2(evecs(2,2),evecs(1,2));
ellipse_x = radii(1)*cos(theta_grid)*cos(phi) - radii(2)*sin(theta_grid)*sin(phi) + center(1);
ellipse_y = radii(1)*cos(theta_grid)*sin(phi) + radii(2)*sin(theta_grid)*cos(phi) + center(2);
ellipse_z = zeros(size(ellipse_x)) + center(3); % 由于是三维数据,置信椭圆位于平面上,Z坐标设为常数
% 绘制置信椭圆
plot3(ellipse_x, ellipse_y, ellipse_z, 'k--', 'LineWidth', 2);
end
legend('Class 1', 'Class 2', 'Class 3', 'Confidence Ellipse');
grid on;
hold off;
存档参考,如需使用,需要大改