Matlab 绘制虹膜数据的主成分图
我们有下面的命令来分析虹膜数据Matlab 绘制虹膜数据的主成分图,matlab,plot,linear-algebra,pca,Matlab,Plot,Linear Algebra,Pca,我们有下面的命令来分析虹膜数据 load fisheriris gscatter(meas(:,1), meas(:,2), species,'rgb'); hold on gscatter(meas(:,3), meas(:,4), species,'rgb');hold on xlabel('Sepal length'); ylabel('Sepal width'); [pc,score,latent,tsquare] = princomp(meas); pc,latent cums
load fisheriris
gscatter(meas(:,1), meas(:,2), species,'rgb'); hold on
gscatter(meas(:,3), meas(:,4), species,'rgb');hold on
xlabel('Sepal length');
ylabel('Sepal width');
[pc,score,latent,tsquare] = princomp(meas);
pc,latent
cumsum(latent)./sum(latent)
我们得到了结果
pc =
0.3614 0.6566 -0.5820 0.3155
-0.0845 0.7302 0.5979 -0.3197
0.8567 -0.1734 0.0762 -0.4798
0.3583 -0.0755 0.5458 0.7537
latent =
4.2282
0.2427
0.0782
0.0238
ans =
0.9246
0.9777
0.9948
1.0000
我们也有数字
我们的任务是
请通过绘制前两个主成分平面图中数据相对于三个类别的三种颜色的投影来显示
我们可以将前两个组件称为
x=pc(:,1);
y=pc(:,2);
但是关于绘图呢?请帮助我,投影到主要组件上的数据在
score
变量中返回,因此绘图简单:
gscatter(score(:,1), score(:,2), species, [], [], [], 'on', 'PC1', 'PC2')
title('Projected Iris data'), grid on
当然,您可以使用EIG
或SVD
:
X = meas;
X = bsxfun(@minus, X, mean(X)); % zero-centered data
[~,S,V] = svd(X,0); % singular value decomposition
[S,ord] = sort(diag(S), 'descend');
pc = V(:,ord); % principle components
latent = S.^2 ./ (size(X,1)-1) % variance explained
score = X*pc; % projected data
投影到主要组件上的数据在
得分变量中返回,因此曲线图仅为:
gscatter(score(:,1), score(:,2), species, [], [], [], 'on', 'PC1', 'PC2')
title('Projected Iris data'), grid on
当然,您可以使用EIG
或SVD
:
X = meas;
X = bsxfun(@minus, X, mean(X)); % zero-centered data
[~,S,V] = svd(X,0); % singular value decomposition
[S,ord] = sort(diag(S), 'descend');
pc = V(:,ord); % principle components
latent = S.^2 ./ (size(X,1)-1) % variance explained
score = X*pc; % projected data
所以它满足这个条件,对吗?请通过绘制前两个主成分平面图中的数据投影来显示三类的三种颜色。@datodatuashvili:是的,它满足这个条件,对吗?请通过绘制前两个主成分平面图中的数据投影来显示这三个类别的三种颜色。@datodatuashvili:是的