1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 最小二乘法和主成分分析的比较 matlab  儿子的papa

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

时间:2020-03-09 07:59:55

相关推荐

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

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

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

clear allclcclf%% compare lsfit and pca % outline:% 01 gen random points in rectangle % 02 rotate and shift rectangle% 03 ls fit% 04 pca % 05 plot result%% main% parameters ---------------------------------------------rng defaultn=5000;% points numberthe=-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=[x_new';y_new']';[coeff,score,latent] = princomp(dat); % new version of matlab, use pca% rotate first PC backeye_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 angleatan(eye_new(1,2)/eye_new(1,1))*180/pi% second PC angleatan(eye_new(2,2)/eye_new(2,1))*180/pi%% 05 plot result------------------------------------------hold onplot(x,y,'b.')% plot ori pointsplot(x_new,y_new,'r.') % plot rotate pointsplot(x_new,y_p,'k-','linewidth',2)% ls fit line plot(x_new,y_p_pca,'g-','linewidth',2) % pca line(first PC)grid onaxis equallegend('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 :

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。