Daniel@0: %DEMKNN1 Demonstrate nearest neighbour classifier. Daniel@0: % Daniel@0: % Description Daniel@0: % The problem consists of data in a two-dimensional space. The data is Daniel@0: % drawn from three spherical Gaussian distributions with priors 0.3, Daniel@0: % 0.5 and 0.2; centres (2, 3.5), (0, 0) and (0,2); and standard Daniel@0: % deviations 0.2, 0.5 and 1.0. The first figure contains a scatter plot Daniel@0: % of the data. The data is the same as in DEMGMM1. Daniel@0: % Daniel@0: % The second figure shows the data labelled with the corresponding Daniel@0: % class given by the classifier. Daniel@0: % Daniel@0: % See also Daniel@0: % DEM2DDAT, DEMGMM1, KNN Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: clc Daniel@0: disp('This program demonstrates the use of the K nearest neighbour algorithm.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: % Generate the test data Daniel@0: ndata = 250; Daniel@0: randn('state', 42); Daniel@0: rand('state', 42); Daniel@0: Daniel@0: [data, c] = dem2ddat(ndata); Daniel@0: Daniel@0: % Randomise data order Daniel@0: data = data(randperm(ndata),:); Daniel@0: Daniel@0: clc Daniel@0: disp('We generate the data in two-dimensional space from a mixture of') Daniel@0: disp('three spherical Gaussians. The centres are shown as black crosses') Daniel@0: disp('in the plot.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: fh1 = figure; Daniel@0: plot(data(:, 1), data(:, 2), 'o') Daniel@0: set(gca, 'Box', 'on') Daniel@0: hold on Daniel@0: title('Data') Daniel@0: hp1 = plot(c(:, 1), c(:,2), 'k+') Daniel@0: % Increase size of crosses Daniel@0: set(hp1, 'MarkerSize', 8); Daniel@0: set(hp1, 'LineWidth', 2); Daniel@0: hold off Daniel@0: Daniel@0: clc Daniel@0: disp('We next use the centres as training examplars for the K nearest') Daniel@0: disp('neighbour algorithm.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: Daniel@0: % Use centres as training data Daniel@0: train_labels = [1, 0, 0; 0, 1, 0; 0, 0, 1]; Daniel@0: Daniel@0: % Label the test data up to kmax neighbours Daniel@0: kmax = 1; Daniel@0: net = knn(2, 3, kmax, c, train_labels); Daniel@0: [y, l] = knnfwd(net, data); Daniel@0: Daniel@0: clc Daniel@0: disp('We now plot each data point coloured according to its classification.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: % Plot the result Daniel@0: fh2 = figure; Daniel@0: colors = ['b.'; 'r.'; 'g.']; Daniel@0: for i = 1:3 Daniel@0: thisX = data(l == i,1); Daniel@0: thisY = data(l == i,2); Daniel@0: hp(i) = plot(thisX, thisY, colors(i,:)); Daniel@0: set(hp(i), 'MarkerSize', 12); Daniel@0: if i == 1 Daniel@0: hold on Daniel@0: end Daniel@0: end Daniel@0: set(gca, 'Box', 'on'); Daniel@0: legend('Class 1', 'Class 2', 'Class 3', 2) Daniel@0: hold on Daniel@0: labels = ['1', '2', '3']; Daniel@0: hp2 = plot(c(:, 1), c(:,2), 'k+'); Daniel@0: % Increase size of crosses Daniel@0: set(hp2, 'MarkerSize', 8); Daniel@0: set(hp2, 'LineWidth', 2); Daniel@0: Daniel@0: test_labels = labels(l(:,1)); Daniel@0: Daniel@0: title('Training data and data labels') Daniel@0: hold off Daniel@0: Daniel@0: disp('The demonstration is now complete: press any key to exit.') Daniel@0: pause Daniel@0: close(fh1); Daniel@0: close(fh2); Daniel@0: clear all; Daniel@0: