函数:

function [T,P,U,Q,B,W] = pls (X,Y,tol2)

% PLS Partial Least Squares Regrassion

%

% [T,P,U,Q,B,Q] = pls(X,Y,tol) performs particial least squares

regrassion

% between the independent variables, X and dependent Y as

% X = T*P' + E;

% Y = U*Q' + F = T*B*Q' + F1;

%

% Inputs:

% X data matrix of independent variables

% Y data matrix of dependent variables

% tol the tolerant of convergence (defaut 1e-10)

%

% Outputs:

% T score matrix of X

% P loading matrix of X

% U score matrix of Y

% Q loading matrix of Y

% B matrix of regression coefficient

% W weight matrix of X

%

% Using the PLS model, for new X1, Y1 can be predicted as

% Y1 = (X1*P)*B*Q' = X1*(P*B*Q')

% or

% Y1 = X1*(W*inv(P'*W)*inv(T'*T)*T'*Y)

%

% Without Y provided, the function will return the principal

components as

% X = T*P' + E

%

% Example: taken from Geladi, P. and Kowalski, B.R., "An example of

2-block

% predictive partial least-squares regression with simulated

data",

% Analytica Chemica Acta, 185(1996) 19--32.

%{

x=[4 9 6 7 7 8 3 2;6 15 10 15 17 22 9 4;8 21 14 23 27 36 15

6;

10 21 14 13 11 10 3 4; 12 27 18 21 21 24 9 6; 14 33 22 29 31 38 15

8;

16 33 22 19 15 12 3 6; 18 39 26 27 25 26 9 8;20 45 30 35 35 40 15

10];

y=[1 1;3 1;5 1;1 3;3 3;5 3;1 5;3 5;5 5];

% leave the last sample for test

N=size(x,1);

x1=x(1:N-1,:);

y1=y(1:N-1,:);

x2=x(N,:);

y2=y(N,:);

% normalization

xmean=mean(x1);

xstd=std(x1);

ymean=mean(y1);

ystd=std(y);

X=(x1-xmean(ones(N-1,1),:))./xstd(ones(N-1,1),:);

Y=(y1-ymean(ones(N-1,1),:))./ystd(ones(N-1,1),:);

% PLS model

[T,P,U,Q,B,W]=pls(X,Y);

% Prediction and error

yp = (x2-xmean)./xstd * (P*B*Q');

fprintf('Prediction error: %g/n',norm(yp-(y2-ymean)./ystd));

%}

%

% By Yi Cao at Cranfield University on 2nd Febuary 2008

%

% Reference:

% Geladi, P and Kowalski, B.R., "Partial Least-Squares Regression:

A

% Tutorial", Analytica Chimica Acta, 185 (1986) 1--7.

%

%

Input check

error(nargchk(1,3,nargin));

error(nargoutchk(0,6,nargout));

if nargin<2

Y=X;

end

tol = 1e-10;

if nargin<3

tol2=1e-10;

end

%

Size of x and y

[rX,cX] = size(X);

[rY,cY] = size(Y);

assert(rX==rY,'Sizes of X and Y mismatch.');

%

Allocate memory to the maximum size

n=max(cX,cY);

T=zeros(rX,n);

P=zeros(cX,n);

U=zeros(rY,n);

Q=zeros(cY,n);

B=zeros(n,n);

W=P;

k=0;

% iteration loop if residual is larger than specfied

while norm(Y)>tol2 && k% choose the column of x has the

largest square of sum as t.

% choose the column of y has the largest square of sum as u.

[dummy,tidx] = max(sum(X.*X));

[dummy,uidx] = max(sum(Y.*Y));

t1 = X(:,tidx);

u = Y(:,uidx);

t = zeros(rX,1);

%

iteration for outer modeling until convergence

while norm(t1-t) > tol

w = X'*u;

w = w/norm(w);

t = t1;

t1 = X*w;

q = Y'*t1;

q = q/norm(q);

u = Y*q;

end

% update p based on t

t=t1;

p=X'*t/(t'*t);

pnorm=norm(p);

p=p/pnorm;

t=t*pnorm;

w=w*pnorm;

% regression and residuals

b = u'*t/(t'*t);

X = X - t*p';

Y = Y - b*t*q';

% save iteration results to outputs:

k=k+1;

T(:,k)=t;

P(:,k)=p;

U(:,k)=u;

Q(:,k)=q;

W(:,k)=w;

B(k,k)=b;

% uncomment the following line if you wish to see the

convergence

% disp(norm(Y))

end

T(:,k+1:end)=[];

P(:,k+1:end)=[];

U(:,k+1:end)=[];

Q(:,k+1:end)=[];

W(:,k+1:end)=[];

B=B(1:k,1:k);

例子:——————————————————————————————————————————————————————————

%%

Principal Component Analysis and Partial Least Squares

% Principal Component Analysis (PCA) and Partial Least Squares

(PLS) are

% widely used tools. This code is to show their relationship

through the

% Nonlinear Iterative PArtial Least Squares (NIPALS) algorithm.

%%

The Eigenvalue and Power Method

% The NIPALS algorithm can be derived from the Power method to

solve the

% eigenvalue problem. Let x be the eigenvector of a square matrix,

A,

% corresponding to the eignvalue s:

%

% $$Ax=sx$$

%

% Modifying both sides by A iteratively leads to

%

% $$A^kx=s^kx$$

%

% Now, consider another vectro y, which can be represented as a

linear

% combination of all eigenvectors:

%

% $$y=/sum_i^n b_ix_i=Xb$$

%

% where

%

% $$X=/left[x_1/,/,/, /cdots/,/,/, x_n /right]$$

%

% and

%

% $$b = /left[b_1/,/,/, /cdots/,/,/, b_n /right]^T$$

%

% Modifying y by A gives

%

% $$Ay=AXb=XSb$$

%

% Where S is a diagnal matrix consisting all eigenvalues.

Therefore, for

% a large enough k,

%

% $$A^ky=XS^kb/approx /alpha x_1$$

%

% That is the iteration will converge to the direction of x_1,

which is the

% eigenvector corresponding to the eigenvalue with the maximum

module.

% This leads to the following Power method to solve the eigenvalue

problem.

A=randn(10,5);

% sysmetric matrix to ensure real eigenvalues

B=A'*A;

%find the column which has the maximum norm

[dum,idx]=max(sum(A.*A));

x=A(:,idx);

%storage to judge convergence

x0=x-x;

%convergence tolerant

tol=1e-6;

%iteration if not converged

while norm(x-x0)>tol

%iteration to approach the eigenvector direction

y=A'*x;

%normalize the vector

y=y/norm(y);

%save previous x

x0=x;

%x is a product of eigenvalue and eigenvector

x=A*y;

end

% the largest eigen value corresponding eigenvector is y

s=x'*x;

% compare it with those obtained with eig

[V,D]=eig(B);

[d,idx]=max(diag(D));

v=V(:,idx);

disp(d-s)

% v and y may be different in signs

disp(min(norm(v-y),norm(v+y)))

%%

The NIPALS Algorithm for PCA

% The PCA is a dimension reduction technique, which is based on

the

% following decomposition:

%

% $$X=TP^T+E$$

%

% Where X is the data matrix (m x n) to be analysed, T is the so

called

% score matrix (m x a), P the loading matrix (n x a) and E the

residual.

% For a given tolerance of residual, the number of principal

components, a,

% can be much smaller than the orginal variable dimension, n.

% The above power algorithm can be extended to get T and P by

iteratively

% subtracting A (in this case, X) by x*y' (in this case, t*p')

until the

% given tolerance satisfied. This is the so called NIPALS

algorithm.

% The

data matrix with normalization

A=randn(10,5);

meanx=mean(A);

stdx=std(A);

X=(A-meanx(ones(10,1),:))./stdx(ones(10,1),:);

B=X'*X;

% allocate T and P

T=zeros(10,5);

P=zeros(5);

% tol for convergence

tol=1e-6;

% tol for PC of 95 percent

tol2=(1-0.95)*5*(10-1);

for k=1:5

%find the column which has the maximum norm

[dum,idx]=max(sum(X.*X));

t=A(:,idx);

%storage to judge convergence

t0=t-t;

%iteration if not converged

while norm(t-t0)>tol

%iteration to approach the eigenvector direction

p=X'*t;

%normalize the vector

p=p/norm(p);

%save previous t

t0=t;

%t is a product of eigenvalue and eigenvector

t=X*p;

end

%subtracing PC identified

X=X-t*p';

T(:,k)=t;

P(:,k)=p;

if norm(X)break

end

end

T(:,k+1:5)=[];

P(:,k+1:5)=[];

S=diag(T'*T);

% compare it with those obtained with eig

[V,D]=eig(B);

[D,idx]=sort(diag(D),'descend');

D=D(1:k);

V=V(:,idx(1:k));

fprintf('The number of PC: %i/n',k);

fprintf('norm of score difference between EIG and NIPALS:

%g/n',norm(D-S));

fprintf('norm of loading difference between EIG and NIPALS:

%g/n',norm(abs(V)-abs(P)));

%%

The NIPALS Algorithm for PLS

% For PLS, we will have two sets of data: the independent X and

dependent

% Y. The NIPALS algorithm can be used to decomposes both X and Y so

that

%

% $$X=TP^T+E,/,/,/,/,Y=UQ^T+F,/,/,/,/,U=TB$$

%

% The regression, U=TB is solved through least sequares whilst

the

% decompsition may not include all components. That is why the

approach is

% called partial least squares. This algorithm is implemented in

the PLS

% function.

%%

Example: Discriminant PLS using the NIPALS Algorithm

% From Chiang, Y.Q., Zhuang, Y.M and Yang, J.Y, "Optimal

Fisher

% discriminant analysis using the rank decomposition", Pattern

Recognition,

% 25 (1992), 101--111.

%

Three classes data, each has 50 samples and 4 variables.

x1=[5.1 3.5 1.4 0.2; 4.9 3.0 1.4 0.2; 4.7 3.2 1.3 0.2; 4.6 3.1 1.5

0.2;...

5.0 3.6 1.4 0.2; 5.4 3.9 1.7 0.4; 4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.0; 6.3 2.7 4.9 1.8;

...

6.7 3.3 5.7 2.1; 7.2 3.2 6.0 1.8; 6.2 2.8 4.8 1.8; 6.1 3.0 4.9 1.8;

...

6.4 2.8 5.6 2.1; 7.2 3.0 5.8 1.6; 7.4 2.8 6.1 1.9; 7.9 3.8 6.4 2.0;

...

6.4 2.8 5.6 2.2; 6.3 2.8 5.1 1.5; 6.1 2.6 5.6 1.4; 7.7 3.0 6.1 2.3;

...

6.3 3.4 5.6 2.4; 6.4 3.1 5.5 1.8; 6.0 3.0 4.8 1.8; 6.9 3.1 5.4 2.1;

...

6.7 3.1 5.6 2.4; 6.9 3.1 5.1 2.3; 5.8 2.7 5.1 1.9; 6.8 3.2 5.9 2.3;

...

6.7 3.3 5.7 2.5; 6.7 3.0 5.2 2.3; 6.3 2.5 5.0 1.9; 6.5 3.0 5.2 2.0;

...

6.2 3.4 5.4 2.3; 5.9 3.0 5.1 1.8];

%Split data set into training (1:25) and testing (26:50)

idxTrain = 1:25;

idxTest = 26:50;

%

Combine training data with normalization

X = [x1(idxTrain,:);x2(idxTrain,:);x3(idxTrain,:)];

% Define class indicator as Y

Y = kron(eye(3),ones(25,1));

% Normalization

xmean = mean(X);

xstd = std(X);

ymean = mean(Y);

ystd = std(Y);

X = (X - xmean(ones(75,1),:))./xstd(ones(75,1),:);

Y = (Y - ymean(ones(75,1),:))./ystd(ones(75,1),:);

% Tolerance for 90 percent score

tol = (1-0.9) * 25 * 4;

% Perform PLS

[T,P,U,Q,B] = pls(X,Y,tol);

% Results

fprintf('Number of components retained: %i/n',size(B,1))

% Predicted classes

X1 = (x1(idxTest,:) -

xmean(ones(25,1),:))./xstd(ones(25,1),:);

X2 = (x2(idxTest,:) -

xmean(ones(25,1),:))./xstd(ones(25,1),:);

X3 = (x3(idxTest,:) -

xmean(ones(25,1),:))./xstd(ones(25,1),:);

Y1 = X1 * (P*B*Q');

Y2 = X2 * (P*B*Q');

Y3 = X3 * (P*B*Q');

Y1 = Y1 .* ystd(ones(25,1),:) + ymean(ones(25,1),:);

Y2 = Y2 .* ystd(ones(25,1),:) + ymean(ones(25,1),:);

Y3 = Y3 .* ystd(ones(25,1),:) + ymean(ones(25,1),:);

% Class is determined from the cloumn which is most close to

1

[dum,classid1]=min(abs(Y1-1),[],2);

[dum,classid2]=min(abs(Y2-1),[],2);

[dum,classid3]=min(abs(Y3-1),[],2);

bar(1:25,classid1,'b');

hold on

bar(26:50,classid2,'r');

bar(51:75,classid3,'g');

hold off

MATLAB实现偏最小,偏最小二乘法 matlab程序相关推荐

  1. matlab 最小一乘法,MATLAB实现最小二乘法

    转载自: https://blog.csdn.net/zengxiantao1994/article/details/70210662 文章仅为创作者的观点,可与转载者讨论 最小二乘法 最小二乘法(又 ...

  2. matalb中的wden函数_小波分析中MATLAB阈值获取函数及其应用附程序代码

    小波分析中MATLAB阈值获取函数及其应用附程序代码 1.小波分析中MATLAB阈值获取函数 MATLAB中实现阈值获取的函数有ddencmp.thselect.wbmpen和wwdcbm,下面对它们 ...

  3. matlab求阈值的函数,小波分析中matlab阈值获取函数及其应用附程序代码.doc

    小波分析中matlab阈值获取函数及其应用附程序代码.doc 1.小波分析中MATLAB阈值获取函数MATLAB中实现阈值获取的函数有DDENCMP.THSELECT.WBMPEN和WWDCBM,下面 ...

  4. matlab 中心最小二乘法,MATLAB最小二乘法

    MATLAB最小二乘法 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 三.实验程序 四.实验内容 设有如下数据: 用3次多项式拟合这组数据. 五.解答 ...

  5. matlab 有一函数 _写一程序_输入自变量的值_输出函数值.,第2讲 MATLAB入门1_数学建模_ppt_大学课件预览_高等教育资讯网...

    数学建模与数学实验 MATLAB入门数学建模及其基于 MATLAB的实现辽宁工程技术大学理学院应用数学系 MATLAB作为线性系统的一种分析和仿真工具,是理工科大学生应该掌握的技术工具,它作为一种编程 ...

  6. matlab 线性回归 参数显著性,如何用MATLAB最小二乘法得出回归方程系数?

    PS:非常抱歉,由于我的疏忽,前面发的一贴,公式写错了.不过还是谢过@dingd.@cooooldog.感谢你们的帮助.这个过程我也有所收获,以后切记马虎. --------------------- ...

  7. 蚁群算法优化神经网络matlab源程序,粒子群优化神经网络的程序大集合

    粒子群程序集合 866867259psobp psobp.m pso(粒子群算法)优化神经网络 粒子群算法(PSO)应用于神经网络优化[matlab] PSOt A Particle Swarm Op ...

  8. matlab中存档算法代码,MATLAB 智能算法超级学习手册中程序代码

    [实例简介] MATLAB 智能算法超级学习手册中程序代码 [实例截图] [核心代码] dc90ef43-7920-434e-bdb8-0636c31c0b44 └── MATLAB 智能算法超级学习 ...

  9. m 文件 dll matlab 中调用_如何在matlab中调用python程序

    现在python很火,很多代码都是python写的,如果你和我一样,习惯了使用matlab,还想在matlab中调用Python的代码,应该怎么办呢?其中一条思路:首先在matlab中调用系统脚本命令 ...

  10. 考虑交通网络流量的电动汽车充电站规划matlab 采用matlab软件参照相关资料完成电动汽车程序

    考虑交通网络流量的电动汽车充电站规划matlab 采用matlab软件参照相关资料完成电动汽车程序,采用粒子群方法,程序本人编制,运行可靠 ID:5868638495393683快乐程序人

最新文章

  1. CSS基本知识1-CSS基本概念
  2. authc过滤器 shiro_使用Shiro实现认证和授权(基于SpringBoot)
  3. python数独游戏源代码_使用Python编写数独游戏自动出题程序
  4. c++读取图片_手工计算神经网络第三期:数据读取与完成训练
  5. 老李分享:接口测试之jmeter
  6. python程序设计之文件_Python程序设计之文件操作(2)
  7. c语言将链表写入二进制文件_通过逐级遍历将二进制树转换为单链表的C程序
  8. 电信业的100个随想
  9. 好看的android动画效果
  10. 南京大学计算机有分学硕专硕,南京大学的学硕和专硕有什么区别吗
  11. 字节跳动李航博士入选2019 ACL Fellow,成为第五位入选华人学者
  12. Java毕设项目博雅楼自习室预约系统计算机(附源码+系统+数据库+LW)
  13. 华为交换机eth口作用_基于华为交换机的基本配置——以Eth-Trunk链路聚合技术为例.pdf...
  14. 测针对精密测量的重要性
  15. MySQL Workbench建表时 PK NN UQ B UN ZF AI G的含义
  16. 稿子文字左右对称翻转_Matlab/OpenCV (2021-09-06)
  17. 微信小程序-图片等比例显示不变形
  18. 使用ffmpeg调整图像大小
  19. 单片机实验笔记(汇编、Proteus仿真)(下)
  20. 《秋波媚·七月十六日晚登高兴亭望长安南山》 陆游

热门文章

  1. iphone尺寸大全对照表2021 iphone屏幕尺寸大全
  2. 好用的电脑端看图软件
  3. 测试笔记本续航的软件,笔记本续航测试
  4. FFmpeg 视频裁剪
  5. (附源码)计算机毕业设计ssm房屋出租管理系统
  6. php网站模板上传教程视频教程,网站模板怎么用
  7. 医院住院管理信息系统类图
  8. Excel常见统计图表汇总
  9. 前端实现3D魔方旋转特效
  10. python实列pdf下载_Python程序实例解析.pdf