meatball1982 发表于 2016-5-4 21:36:47

最小二乘法和主成分分析的比较 matlab  儿子的papa

比较ls和pca,实际上,当点比较少的时候,最小二乘法是个不错的选择,但要观察数据的大体方向,pca是个更好的选择。下面说明。

生成在一定范围内的矩形数据,下图中的蓝色的点(我暂时只生成5000个点。),将这些点进行旋转(这时是-30度),得到图中的红色点。分别用ls和pca分析得到红色点的主要方向。

clear all
clc
clf
%% compare lsfit andpca
% outline:
% 01 gen random points in rectangle
% 02 rotate and shift rectangle
% 03 ls fit
% 04 pca
% 05 plot result

%% main
% parameters ---------------------------------------------
rng default
n=5000;          % points number
the=-pi/6;       % rotate angle

%% 01 gen random points in rectangle ----------------------
x=rand(n,1)*4-2;
y=rand(n,1)*3-1.5;

%% 02 rotate and shift rectangle --------------------------
x_new= cos(the)*x+sin(the)*y+3;
y_new=-sin(the)*x+cos(the)*y+5;

%% 03 ls fit ----------------------------------------------
p=polyfit(x_new,y_new,1);
y_p=p(1)*x_new+p(2);

%% 04 pca -------------------------------------------------
dat=';
= princomp(dat); % new version of matlab, use pca

% rotate first PC back
eye_mat=eye(2);
eye_new=eye_mat*(coeff)^(-1);

% gen pca line
x_cen=mean(x_new);
y_cen=mean(y_new);
y_p_pca=eye_new(1,2)/eye_new(1,1)*(x_new-x_cen)+y_cen;

% first PC angle
atan(eye_new(1,2)/eye_new(1,1))*180/pi

% second PC angle
atan(eye_new(2,2)/eye_new(2,1))*180/pi

%% 05 plot result------------------------------------------
hold on
plot(x,y,'b.')                        % plot ori points
plot(x_new,y_new,'r.')                  % plot rotate points
plot(x_new,y_p,'k-','linewidth',2)      % ls fit line
plot(x_new,y_p_pca,'g-','linewidth',2)% pca line(first PC)

grid on
axis equal
legend('ori point','rot point','ls fit','PCA','Location','southeast')

% option: output fig
% h=gcf;
% fig_na=['../imgs/fig_01_comp_lsfit_pca'];
% fun_work_li_035_myfig_out(h,fig_na,3);

%% logs
% mod :

页: [1]
查看完整版本: 最小二乘法和主成分分析的比较 matlab  儿子的papa