我想用前三个主要成分制作一个 3D PCA 图,并为每个类(标签)设置一个 95% 的置信椭圆体。这是我的代码:
% Input data
InputData = [7.72 6.73 3.33 0.12 0.06 -0.31 1;
8.92 8.22 4.56 0.06 0.72 0.01 1;
2.11 1.59 0.7 0.12 0.67 -0.02 1;
79.42 92.88 75.03 64.98 67.8 55.93 2;
100.82 112.5 94.7 73.16 74.22 64.15 2;
69.43 83.4 67.46 80.78 81.36 69.38 2;
25.25 23.86 17.95 0.12 0.44 0.14 3;
39.97 36.9 30.06 0.13 0.82 0.35 3;
31.99 29.48 23.68 -0.12 0.53 -0.1 3;
129.93 126.54 113.51 1.09 0.17 0.38 4;
129.91 127.4 114.6 1.32 1.05 1.19 4;
110.38 108.7 96.3 0.41 1.25 0.87 4];
% Extract the features (first 6 columns) and labels (last column) from the data
X = InputData(:,1:6);
labels = InputData(:,7);
% Perform PCA on the data
[coeff,score,latent,~,explained] = pca(X);
% Get the first three principal components
PC1 = score(:,1);
PC2 = score(:,2);
PC3 = score(:,3);
% Calculate the means for each label
mean1 = mean(score(labels==1,1:3));
mean2 = mean(score(labels==2,1:3));
mean3 = mean(score(labels==3,1:3));
mean4 = mean(score(labels==4,1:3));
% Calculate the covariance matrices for each label
cov1 = cov(score(labels==1,1:3));
cov2 = cov(score(labels==2,1:3));
cov3 = cov(score(labels==3,1:3));
cov4 = cov(score(labels==4,1:3));
% Set the confidence level for the ellipsoids
confLevel = 0.95;
% Create a 3D scatter plot with different colors for each label
figure
scatter3(PC1(labels==1), PC2(labels==1), PC3(labels==1), 'r', 'filled')
hold on
scatter3(PC1(labels==2), PC2(labels==2), PC3(labels==2), 'g', 'filled')
scatter3(PC1(labels==3), PC2(labels==3), PC3(labels==3), 'b', 'filled')
scatter3(PC1(labels==4), PC2(labels==4), PC3(labels==4), 'm', 'filled')
title('3D Scatter Plot with 95% Confidence Ellipsoids')
xlabel('PC1')
ylabel('PC2')
zlabel('PC3')
legend('Label 1', 'Label 2', 'Label 3', 'Label 4')
% Create the ellipsoids for each label
drawEllipsoid(mean1, cov1, confLevel, 'r')
drawEllipsoid(mean2, cov2, confLevel, 'g')
drawEllipsoid(mean3, cov3, confLevel, 'b')
drawEllipsoid(mean4, cov4, confLevel, 'm')
% Function to draw ellipsoids
function drawEllipsoid(mean, covMatrix, confLevel, color)
% Get the eigenvalues and eigenvectors of the covariance matrix
[V,D] = eig(covMatrix);
% Calculate the radii of the ellipsoid
radii = sqrt(diag(D) * finv(confLevel, 3, size(mean,2)-1));
% Generate points on a unit sphere
[x,y,z] = sphere;
% Transform the points using the eigenvectors and radii
points = [x(:) y(:) z(:)] * diag(radii) * V';
% Add the mean to the points
points = points + mean;
% Reshape the points into a matrix
points = reshape(points, size(x,1), size(x,2), size(x,3));
% Plot the ellipsoid
h = surf(points(:,:,1), points(:,:,2), points(:,:,3));
set(h, 'FaceColor', color, 'FaceAlpha', 0.1, 'EdgeAlpha', 0.1)
end
Matlab 告诉我 drawEllipsoid 函数中有错误,但我无法修复它或理解错误所在。 错误说:
使用 reshape 时出错元素数量不得更改。使用 [] 作为一个 尺寸输入自动计算合适的尺寸 那个维度。
test_3Dpcav2>drawEllipsoid 错误(第 83 行) points = reshape(points, size(x,1), size(x,2), size(x,3));
test_3Dpcav2 错误(第 60 行)drawEllipsoid(mean1, cov1, confLevel, 'r')
有人可以解释和/或展示如何解决它吗?