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

📄 test_ams.m

📁 MATLAB2007科学计算与工程分析代码
💻 M
字号:
function test_ams()  n = 200;   % Number of training points  nt = 1000; % Number of test points    % We construct a 2D toy problem which looks like a checkboard.  % But there is more variation on the 2nd component (and the length  %   scale should be smaller)  X = rand(n,2);  Y = sign((X(:,1)-0.5).*(mod(3*X(:,2),1)-0.5));  Xt = rand(nt,2);  Yt = sign((Xt(:,1)-0.5).*(mod(3*Xt(:,2),1)-0.5));      % Normalize by the standard deviation on each component  s = std(X);  X  = X./repmat(s,n,1);  Xt = Xt./repmat(s,nt,1);    % Plot the toy problem  plot(X(Y== 1,1),X(Y== 1,2),'b+'); hold on;    plot(X(Y==-1,1),X(Y==-1,2),'ro'); hold off; pause(1); drawnow;    % We now try the four different model selection criterion  meth = {'loo' 'rm' 'val' 'ev'};  for i=1:4    [sig,C,alpha,b] = ams(X,Y,meth{i},0); % Do the model selection    Kt = compute_kernel(Xt,X,sig); % Compute the test kernel...    te = mean(Yt.*(Kt*alpha+b)<0); % ... and the test error    fprintf('Method = %s,\t sigma_1 = %.3f, sigma_2 = %.3f, test error = %.3f\n',...            meth{i},sig(1),sig(2),te);  end;    fprintf(['All methods give comparable error rates and find that the ' ...           'length scale should be smaller on the 2nd component.\n']);function K = compute_kernel(X,Y,sig)  % Compute the RBF kernel matrix  if length(sig)==1    X = X / sig;    Y = Y / sig;  else    X = X ./ repmat(sig',size(X,1),1);    Y = Y ./ repmat(sig',size(Y,1),1);  end;  normX = sum(X.^2,2);  normY = sum(Y.^2,2);  K = exp(-0.5*(repmat(normX ,1,size(Y,1)) + ...                repmat(normY',size(X,1),1) - 2*X*Y')); 

⌨️ 快捷键说明

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