Daniel@0: %DEMKMEAN Demonstrate simple clustering model trained with K-means. 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: % A cluster model with three components is trained using the batch K- Daniel@0: % means algorithm. The matrix of centres is printed after training. The Daniel@0: % second figure shows the data labelled with a colour derived from the Daniel@0: % corresponding cluster Daniel@0: % Daniel@0: % See also Daniel@0: % DEM2DDAT, DEMGMM1, KNN1, KMEANS Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: % Generate the data, fixing seeds for reproducible results Daniel@0: ndata = 250; Daniel@0: randn('state', 42); Daniel@0: rand('state', 42); Daniel@0: data = dem2ddat(ndata); Daniel@0: Daniel@0: % Randomise data order Daniel@0: data = data(randperm(ndata),:); Daniel@0: Daniel@0: clc Daniel@0: disp('This demonstration illustrates the use of a cluster model to') Daniel@0: disp('find centres that reflect the distribution of data points.') Daniel@0: disp('We begin by generating the data from a mixture of three Gaussians') Daniel@0: disp('in two-dimensional space and plotting it.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: Daniel@0: fh1 = figure; Daniel@0: plot(data(:, 1), data(:, 2), 'o') Daniel@0: set(gca, 'Box', 'on') Daniel@0: title('Data') Daniel@0: Daniel@0: % Set up cluster model Daniel@0: ncentres = 3; Daniel@0: centres = zeros(ncentres, 2); Daniel@0: Daniel@0: % Set up vector of options for kmeans trainer Daniel@0: options = foptions; Daniel@0: options(1) = 1; % Prints out error values. Daniel@0: options(5) = 1; Daniel@0: options(14) = 10; % Number of iterations. Daniel@0: Daniel@0: clc Daniel@0: disp('The model is chosen to have three centres, which are initialised') Daniel@0: disp('at randomly selected data points. We now train the model using') Daniel@0: disp('the batch K-means algorithm with a maximum of 10 iterations and') Daniel@0: disp('stopping tolerance of 1e-4.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: Daniel@0: % Train the centres from the data Daniel@0: [centres, options, post] = kmeans(centres, data, options); Daniel@0: Daniel@0: % Print out model Daniel@0: disp(' ') Daniel@0: disp('Note that training has terminated before 10 iterations as there') Daniel@0: disp('has been no change in the centres or error function.') Daniel@0: disp(' ') Daniel@0: disp('The trained model has centres:') Daniel@0: disp(centres); Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: Daniel@0: clc Daniel@0: disp('We now plot each data point coloured according to its classification') Daniel@0: disp('given by the nearest cluster centre. The cluster centres are denoted') Daniel@0: disp('by black crosses.') Daniel@0: Daniel@0: % Plot the result Daniel@0: fh2 = figure; Daniel@0: Daniel@0: hold on Daniel@0: colours = ['b.'; 'r.'; 'g.']; Daniel@0: Daniel@0: [tempi, tempj] = find(post); Daniel@0: hold on Daniel@0: for i = 1:3 Daniel@0: % Select data points closest to ith centre Daniel@0: thisX = data(tempi(tempj == i), 1); Daniel@0: thisY = data(tempi(tempj == i), 2); Daniel@0: hp(i) = plot(thisX, thisY, colours(i,:)); Daniel@0: set(hp(i), 'MarkerSize', 12); 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: plot(centres(:, 1), centres(:,2), 'k+', 'LineWidth', 2, ... Daniel@0: 'MarkerSize', 8) Daniel@0: title('Centres and data labels') Daniel@0: hold off Daniel@0: Daniel@0: disp(' ') Daniel@0: disp('Press any key to end.') Daniel@0: pause Daniel@0: Daniel@0: close(fh1); Daniel@0: close(fh2); Daniel@0: clear all; Daniel@0: