You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
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 ) ;