From 7c100f95fae7b5e3b734a93ea3ec9d1d7ef1dd43 Mon Sep 17 00:00:00 2001 From: phzrjyvu9 <2026074240@qq.com> Date: Sun, 21 Jul 2024 22:53:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E6=88=90=E5=88=86=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 姚安欣/PCA.m | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 姚安欣/PCA.m diff --git a/姚安欣/PCA.m b/姚安欣/PCA.m new file mode 100644 index 0000000..cd33326 --- /dev/null +++ b/姚安欣/PCA.m @@ -0,0 +1,96 @@ +clc,clear,format short,close all; +load data_mh.mat; +[n,p]=size(x); + +%标准化 +X=zscore(x); + +%协方差矩阵/相关系数矩阵 +R=cov(X); +[V,D]=eig(R);%[特征向量,特征值] + +lambda=diag(D); +lambda=lambda(end:-1:1); + +total_contri=sum(lambda); + +cum_contri=cumsum(lambda); + +contri_rate=cum_contri/total_contri; + +V1=rot90(V)'; +disp(V1); + +c1=V1(:,1); +c2=V1(:,2); + +XX=X*c1; +YY=X*c2; + + +figure; +scatter(XX, YY, 'filled'); +xlabel('第一主成分'); +ylabel('第二主成分'); +title('主成分得分图'); +grid on; + + + + + + +%散点 +figure; +scatter(XX,YY,'k.'); + + +figure; +plot(c1, c2, 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'b'); +text(c1, c2, cellstr(num2str((1:p)')), 'VerticalAlignment','bottom', 'HorizontalAlignment','right'); +xlabel('第一主成分'); +ylabel('第二主成分'); +title('主成分载荷图'); +grid on; + + + + + + + + + + + + +%主成分得分图(Score Plot) +X = randn(100, 5); + +% 主成分分析 +[coeff, score, latent, tsquared, explained, mu] = pca(X); + +% 绘制前两个主成分的得分图 +figure; +scatter(score(:,1), score(:,2)); +xlabel('第一主成分'); +ylabel('第二主成分'); +title('主成分得分图'); +grid on; + +% 添加数据点标签 +text(score(:,1), score(:,2), num2str((1:size(score,1))'), 'FontSize', 8); + + + + + + + + + + + + + +