PCA MATLAB代码

clear all,clc;
A=[[-1,1];[-2,-1];[-3,-2];[1,1];[2,1];[3,2]];
A_mean=A-mean(A);
A_div=A_mean;
M=A_div'*A_div;
N=A_div*A_div';
[M_vector,M_val]=eig(M);
[N_vector,N_val]=eig(N);

M_vector=fliplr(M_vector);
N_vector=fliplr(N_vector);
%M_vector=flipud(M_vector)
%N_vector=flipud(N_vector)

M_val=diag(M_val);
N_val=diag(N_val);
M_val=flipud(M_val)
N_val=flipud(N_val)

theta1=sqrt(M_val(1));
theta2=sqrt(M_val(2));
cgma=zeros(size(A));
cgma(1,1)=theta1;
cgma(2,2)=theta2;
%-(N_vector*cgma*M_vector')
-N_vector*cgma
[clo,row]=size(A)
final_output=-N_vector(:,1)*sqrt((clo-1))
explained_variance_=sum(cgma).*sum(cgma)./(clo-1);
reverse=final_output*(sqrt(explained_variance_(1)).*M_vector(:,1)')+mean(A)

  

 

 

clear,clc;
A=[[0,1];[1,1];[1,0]];
A_mean=A;
%A_div=A_mean*A_mean';
A_div=A_mean;
M=A_div'*A_div;
N=A_div*A_div';
[M_vector,M_val]=eig(M);
[N_vector,N_val]=eig(N);
M_vector=fliplr(M_vector);
N_vector=fliplr(N_vector);
M_vector=flipud(M_vector)
N_vector=flipud(N_vector)
M_val=diag(M_val);
N_val=diag(N_val);
M_val=flipud(M_val)
N_val=flipud(N_val)
theta1=A_mean*M_vector(:,1)./N_vector(:,1)
theta2=A_mean*M_vector(:,2)./N_vector(:,2)
cgma=zeros(size(A));
cgma(1,1)=theta1(1);
cgma(2,2)=theta2(1);
N_vector*cgma*M_vector'

  

 

 

clear,clc;
A=[[-1,1];[-2,-1];[-3,-2];[1,1];[2,1];[3,2]];
A_mean=A-mean(A);
A_div=A_mean*A_mean';
A_div=A_mean;
M=A_div'*A_div;
N=A_div*A_div';
[M_vector,M_val]=eig(M);
[N_vector,N_val]=eig(N);

M_vector=fliplr(M_vector);
N_vector=fliplr(N_vector);
%M_vector=flipud(M_vector)
%N_vector=flipud(N_vector)

M_val=diag(M_val);
N_val=diag(N_val);
M_val=flipud(M_val)
N_val=flipud(N_val)

theta1=sqrt(M_val(1));
theta2=sqrt(M_val(2));
cgma=zeros(size(A));
cgma(1,1)=theta1;
cgma(2,2)=theta2;
-(N_vector*cgma*M_vector')
N_vector*cgma

  

 

早期的代码备份,这样文件删的心安理得。

上一篇:day2021_9_26


下一篇:主成分分析(PCA)原理