⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pls.m

📁 《模式分析的核方法》一书中的源代码
💻 M
字号:
function [W,testY,testerror] = pls(X,Xtest,Y,T,varargin)%function [W,testY,testerror] = pls(X,Xtest,Y,T,varargin)%% Performs PLS discrimination%%INPUTS% X = the training kernel matrix (ell x N)% Xtest = the sample matrix (elltest x N)% Y = the training label matrix (ell x m)% T = the number of PLS components to take% varargin = optional argument specifying the true test label matrix%            of size elltest x m%%OUTPUTs% w = the weight vectors corresponding to the PLS classfier% testY = the estimated label matrix for the test samples% testerror = the test error%%%For more info, see www.kernel-methods.net%%Note: this code has not been tested extensively.% X is an ell x n matrix whose rows are the training inputs% Y is ell x m containing the corresponding output vectors% T gives the number of iterations to be performedmux = mean(X); muy = mean(Y); jj = ones(size(X,1),1);X = X - jj*mux; Y = Y - jj*muy;trainY=0;ell=size(X,1);for i=1:T    YX = Y'*X;    u(:,i) = YX(1,:)'/norm(YX(1,:));    if size(Y,2) > 1, % only loop if dimension greater than 1        uold = u(:,i) + 1;        while norm(u(:,i) - uold) > 0.001,            uold = u(:,i);            tu = YX'*YX*u(:,i);            u(:,i) = tu/norm(tu);        end    end    t = X*u(:,i);    c(:,i) = Y'*t/(t'*t);    p(:,i) = X'*t/(t'*t);    trainY = trainY + t*c(:,i)';    trainerror = norm(Y - trainY,'fro')/sqrt(ell);    X = X - t*p(:,i)';    % compute residual Y = Y - t*c(:,i)';end% Regression coefficients for new dataW = u * ((p'*u)\c');%  Xtest gives new data inputs as rows, Ytest true outputselltest = size(Xtest,1); jj = ones(elltest,1);testY = (Xtest - jj*mux) * W + jj*muy;if ~isempty(varargin)    Ytest = varargin{1};    testerror = norm(Ytest - testY,'fro')/sqrt(elltest)    varargout = testerror;end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -