Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/netlab3.3/demknn1.m @ 0:e9a9cd732c1e tip
first hg version after svn
| author | wolffd |
|---|---|
| date | Tue, 10 Feb 2015 15:05:51 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:e9a9cd732c1e |
|---|---|
| 1 %DEMKNN1 Demonstrate nearest neighbour classifier. | |
| 2 % | |
| 3 % Description | |
| 4 % The problem consists of data in a two-dimensional space. The data is | |
| 5 % drawn from three spherical Gaussian distributions with priors 0.3, | |
| 6 % 0.5 and 0.2; centres (2, 3.5), (0, 0) and (0,2); and standard | |
| 7 % deviations 0.2, 0.5 and 1.0. The first figure contains a scatter plot | |
| 8 % of the data. The data is the same as in DEMGMM1. | |
| 9 % | |
| 10 % The second figure shows the data labelled with the corresponding | |
| 11 % class given by the classifier. | |
| 12 % | |
| 13 % See also | |
| 14 % DEM2DDAT, DEMGMM1, KNN | |
| 15 % | |
| 16 | |
| 17 % Copyright (c) Ian T Nabney (1996-2001) | |
| 18 | |
| 19 clc | |
| 20 disp('This program demonstrates the use of the K nearest neighbour algorithm.') | |
| 21 disp(' ') | |
| 22 disp('Press any key to continue.') | |
| 23 pause | |
| 24 % Generate the test data | |
| 25 ndata = 250; | |
| 26 randn('state', 42); | |
| 27 rand('state', 42); | |
| 28 | |
| 29 [data, c] = dem2ddat(ndata); | |
| 30 | |
| 31 % Randomise data order | |
| 32 data = data(randperm(ndata),:); | |
| 33 | |
| 34 clc | |
| 35 disp('We generate the data in two-dimensional space from a mixture of') | |
| 36 disp('three spherical Gaussians. The centres are shown as black crosses') | |
| 37 disp('in the plot.') | |
| 38 disp(' ') | |
| 39 disp('Press any key to continue.') | |
| 40 pause | |
| 41 fh1 = figure; | |
| 42 plot(data(:, 1), data(:, 2), 'o') | |
| 43 set(gca, 'Box', 'on') | |
| 44 hold on | |
| 45 title('Data') | |
| 46 hp1 = plot(c(:, 1), c(:,2), 'k+') | |
| 47 % Increase size of crosses | |
| 48 set(hp1, 'MarkerSize', 8); | |
| 49 set(hp1, 'LineWidth', 2); | |
| 50 hold off | |
| 51 | |
| 52 clc | |
| 53 disp('We next use the centres as training examplars for the K nearest') | |
| 54 disp('neighbour algorithm.') | |
| 55 disp(' ') | |
| 56 disp('Press any key to continue.') | |
| 57 pause | |
| 58 | |
| 59 % Use centres as training data | |
| 60 train_labels = [1, 0, 0; 0, 1, 0; 0, 0, 1]; | |
| 61 | |
| 62 % Label the test data up to kmax neighbours | |
| 63 kmax = 1; | |
| 64 net = knn(2, 3, kmax, c, train_labels); | |
| 65 [y, l] = knnfwd(net, data); | |
| 66 | |
| 67 clc | |
| 68 disp('We now plot each data point coloured according to its classification.') | |
| 69 disp(' ') | |
| 70 disp('Press any key to continue.') | |
| 71 pause | |
| 72 % Plot the result | |
| 73 fh2 = figure; | |
| 74 colors = ['b.'; 'r.'; 'g.']; | |
| 75 for i = 1:3 | |
| 76 thisX = data(l == i,1); | |
| 77 thisY = data(l == i,2); | |
| 78 hp(i) = plot(thisX, thisY, colors(i,:)); | |
| 79 set(hp(i), 'MarkerSize', 12); | |
| 80 if i == 1 | |
| 81 hold on | |
| 82 end | |
| 83 end | |
| 84 set(gca, 'Box', 'on'); | |
| 85 legend('Class 1', 'Class 2', 'Class 3', 2) | |
| 86 hold on | |
| 87 labels = ['1', '2', '3']; | |
| 88 hp2 = plot(c(:, 1), c(:,2), 'k+'); | |
| 89 % Increase size of crosses | |
| 90 set(hp2, 'MarkerSize', 8); | |
| 91 set(hp2, 'LineWidth', 2); | |
| 92 | |
| 93 test_labels = labels(l(:,1)); | |
| 94 | |
| 95 title('Training data and data labels') | |
| 96 hold off | |
| 97 | |
| 98 disp('The demonstration is now complete: press any key to exit.') | |
| 99 pause | |
| 100 close(fh1); | |
| 101 close(fh2); | |
| 102 clear all; | |
| 103 |
