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