Attachment 'sheet07.m'

Download

   1 function sheet07
   2 
   3 N = 300; % size of training data set
   4 K = 5;   % number of neighbors
   5 
   6 % generate a training and test data set
   7 [X, Y] = generate_data(N);
   8 [XE, YE] = generate_data(1000);
   9 
  10 % plot training data set
  11 IP = find(Y == 1);
  12 IN = find(Y == -1);
  13 plot(X(IP, 1), X(IP, 2), 'r+', X(IN, 1), X(IN, 2), 'bo');
  14 
  15 % predict on training set
  16 Yh = knn(K, X, Y, X);
  17 
  18 % predict on test set
  19 YEh = knn(K, X, Y, XE);
  20 
  21 % Plot predictions and training and test error in the title.
  22 hold on;
  23 plotgrid(K, X, Y, -6, 6, -6, 6);
  24 hold off;
  25 colormap([0 0 0])
  26 title(sprintf('training error = %.2f%%, test error = %.2f%%', ...
  27               loss(Y, Yh), loss(YE, YEh)));
  28 
  29 function [X, Y] = generate_data(N)
  30 X = [2*randn(N, 2) + repmat([1, 2], N, 1); ...
  31      0.5*randn(N, 2) + repmat([-2, 1], N, 1)];
  32 Y = [ones(N, 1); -ones(N, 1)];
  33 
  34 % Compute all pairwise distances quickly.
  35 function D = pwdist(X, Y)
  36 D = size(X, 2);
  37 N = size(X, 1);
  38 M = size(Y, 1);
  39   
  40 XX = sum(X.*X, 2);
  41 YY = sum(Y.*Y, 2);
  42 D = repmat(XX, 1, M) + repmat(YY', N, 1) - 2*X*Y';
  43 
  44 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  45 % Your Solutions Below
  46 
  47 % 1. knn - implement k-nearest neighbor prediction
  48 %
  49 % inputs:
  50 %   K - number of neighbors
  51 %   X - [n, d]-matrix with n training data points of dimension d (in the rows!)
  52 %   Y - [n, 1]-matrix of +1, -1 training labels
  53 %   XE - [m, d]-matrix with test labels, for which predictions should
  54 %        be computed
  55 %
  56 % output:
  57 %   Yh - [m, 1]-matrix of predicted labels (real number, sign
  58 %        indicates class.
  59 %
  60 % Note: one for-loop is allowed.
  61 function Yh = knn(K, X, Y, XE)
  62 % ...
  63 
  64 % 2. loss - compute the 0-1 loss. Sign of entries of Y and Yh indicate
  65 % class.
  66 function E = loss(Y, Yh)
  67 % ...
  68 
  69 % 3. plotgrid - plot knn predictions on a grid
  70 %
  71 % inputs:
  72 %   K, X, Y - as inputs to knn
  73 %   XMIN, XMAX - minimal and maximal value of X
  74 %   YMIN, YMAX - minimal and maximal value of Y
  75 %
  76 % Hint: use meshgrid, contour
  77 function plotgrid(K, X, Y, XMIN, XMAX, YMIN, YMAX)
  78 % ...

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-02-04 11:33:18, 583.4 KB) [[attachment:EM-algo_slides.pdf]]
  • [get | view] (2009-11-20 09:49:15, 73.5 KB) [[attachment:FDA_and_LSR.pdf]]
  • [get | view] (2010-01-28 15:25:54, 129.5 KB) [[attachment:bayes_net_slides.pdf]]
  • [get | view] (2010-01-27 14:33:46, 54.2 KB) [[attachment:blatt14.pdf]]
  • [get | view] (2010-02-04 09:40:59, 66.6 KB) [[attachment:blatt15.pdf]]
  • [get | view] (2010-02-04 11:32:30, 1407.4 KB) [[attachment:clustering_slides.pdf]]
  • [get | view] (2009-10-19 10:30:57, 61.5 KB) [[attachment:full_sheet01.pdf]]
  • [get | view] (2009-10-22 09:45:29, 56.1 KB) [[attachment:full_sheet02.pdf]]
  • [get | view] (2009-10-23 20:39:28, 61.4 KB) [[attachment:full_sheet03.pdf]]
  • [get | view] (2009-11-05 10:38:47, 88.7 KB) [[attachment:full_sheet04.pdf]]
  • [get | view] (2009-11-12 11:38:12, 97.3 KB) [[attachment:full_sheet05.pdf]]
  • [get | view] (2009-11-22 14:02:25, 78.4 KB) [[attachment:full_sheet06.pdf]]
  • [get | view] (2009-11-25 14:30:20, 77.3 KB) [[attachment:full_sheet07.pdf]]
  • [get | view] (2009-12-03 13:44:22, 83.1 KB) [[attachment:full_sheet08.pdf]]
  • [get | view] (2009-12-10 20:31:09, 82.6 KB) [[attachment:full_sheet09.pdf]]
  • [get | view] (2009-12-18 15:19:15, 63.2 KB) [[attachment:full_sheet10.pdf]]
  • [get | view] (2010-01-07 10:31:31, 73.3 KB) [[attachment:full_sheet11.pdf]]
  • [get | view] (2010-01-18 11:45:00, 68.1 KB) [[attachment:full_sheet12.pdf]]
  • [get | view] (2010-01-21 14:25:29, 60.4 KB) [[attachment:full_sheet13.pdf]]
  • [get | view] (2009-12-18 15:04:55, 1221.6 KB) [[attachment:lect_kernelmethods.pdf]]
  • [get | view] (2009-12-10 20:35:27, 100.3 KB) [[attachment:p-values.pdf]]
  • [get | view] (2009-11-05 10:38:53, 0.8 KB) [[attachment:sheet04.m]]
  • [get | view] (2009-11-12 10:52:42, 1.4 KB) [[attachment:sheet05.m]]
  • [get | view] (2009-11-22 14:00:59, 1.1 KB) [[attachment:sheet06.m]]
  • [get | view] (2009-11-25 14:30:27, 1.9 KB) [[attachment:sheet07.m]]
  • [get | view] (2009-12-03 13:44:29, 2.7 KB) [[attachment:sheet08.m]]
  • [get | view] (2009-12-10 20:31:16, 1.2 KB) [[attachment:sheet09.m]]
  • [get | view] (2010-01-07 10:32:46, 1.6 KB) [[attachment:sheet11.m]]
  • [get | view] (2010-01-07 10:31:38, 4.8 KB) [[attachment:sheet11_data.m]]
 All files | Selected Files: delete move to page copy to page

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