Attachment 'U04_test_CV_krr.m'

Download

   1 function U04_test_CV_krr
   2 
   3 % Generate data.
   4 n = 20;
   5 x = linspace(-1, 1, n);
   6 y = [ 1.153741e+00, 8.145612e-01, 4.369920e-01, 4.227247e-01, 2.699726e-01, 2.347085e-01, 1.136709e-01, 4.134774e-02, -4.843545e-02, -3.683275e-03, -1.416303e-01, 8.616478e-02, -6.309822e-02, 6.957640e-02, 2.097653e-01, 3.599886e-01, 4.604808e-01, 7.970857e-01, 9.627512e-01, 1.062644e+00 ];
   7 
   8 % Plot data. 
   9 clf; hold on;
  10 plot(x,y,'bo');
  11 
  12 % Select kernel width sigma using 10x10-CV using squared loss. 
  13 [M,opt_param] = cv(x,y,@(tr_x,tr_y,sigma) krr(tr_x,tr_y,'gaussian',sigma), ...
  14         { 'sigma', logspace(0,1,10) }, [], [], @loss_squared);
  15 
  16 % Compute and plot predictions.
  17 xx = linspace(-1,1,200);
  18 yy = apply(M,xx);
  19 plot(xx,yy,'r--');
  20 
  21 % Plot truth. 
  22 plot(xx,f(xx),'b--');
  23 
  24 legend('Data', 'Predictions', 'Truth');
  25 title(sprintf('Kernel width selected by CV: %f', opt_param.sigma));
  26 
  27 % Squared loss.
  28 function sqloss = loss_squared(X,y_true,y_pred)
  29 sqloss = y_true-y_pred;
  30 sqloss = sum(sqloss(:).^2);
  31 
  32 % True regression function.
  33 function y = f(x) 
  34 y = x.^2;
  35 
  36 % Apply function.
  37 function y = apply(M,X)
  38 y = feval(M.applyfunc, M, X);

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2010-04-14 16:11:55, 64.5 KB) [[attachment:ML_Praktikum_U01.pdf]]
  • [get | view] (2010-04-20 12:09:43, 77.0 KB) [[attachment:ML_Praktikum_U02.pdf]]
  • [get | view] (2010-05-10 10:05:42, 66.6 KB) [[attachment:ML_Praktikum_U03.pdf]]
  • [get | view] (2010-06-22 21:26:11, 157.1 KB) [[attachment:ML_Praktikum_U04.pdf]]
  • [get | view] (2010-06-28 07:56:26, 124.2 KB) [[attachment:ML_Praktikum_U05.pdf]]
  • [get | view] (2010-03-30 07:52:44, 65.8 KB) [[attachment:Praktikum_ML_Info_SS10.pdf]]
  • [get | view] (2010-04-14 16:13:22, 1.2 KB) [[attachment:U01_test_distmat.m]]
  • [get | view] (2010-04-14 16:13:25, 1.0 KB) [[attachment:U01_test_mydet.m]]
  • [get | view] (2010-04-23 10:12:56, 4053.5 KB) [[attachment:U01_usps.mat]]
  • [get | view] (2010-04-20 12:14:45, 39.4 KB) [[attachment:U02_tests.zip]]
  • [get | view] (2010-05-25 10:11:01, 6.3 KB) [[attachment:U03_2gaussians.dat]]
  • [get | view] (2010-05-25 10:11:10, 15.6 KB) [[attachment:U03_5gaussians.dat]]
  • [get | view] (2010-05-25 11:05:44, 8.9 KB) [[attachment:U03_tests.zip]]
  • [get | view] (2010-06-07 09:33:08, 1018.4 KB) [[attachment:U04_datasets.tar.gz]]
  • [get | view] (2010-06-07 09:32:04, 1.1 KB) [[attachment:U04_test_CV_krr.m]]
  • [get | view] (2010-06-28 09:40:50, 6.4 KB) [[attachment:U05_datasets.zip]]
  • [get | view] (2010-04-23 10:10:14, 60.1 KB) [[attachment:fishbowl.mat]]
  • [get | view] (2010-04-23 10:10:18, 22.7 KB) [[attachment:flatroll.mat]]
  • [get | view] (2010-04-14 16:10:51, 371.8 KB) [[attachment:guide.pdf]]
  • [get | view] (2010-04-28 09:06:05, 496.0 KB) [[attachment:lle_talk.pdf]]
  • [get | view] (2010-04-23 10:10:23, 45.3 KB) [[attachment:swissroll.mat]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.