annotate toolboxes/FullBNT-1.0.7/netlab3.3/demkmn1.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 %DEMKMEAN Demonstrate simple clustering model trained with K-means.
Daniel@0 2 %
Daniel@0 3 % Description
Daniel@0 4 % The problem consists of data in a two-dimensional space. The data is
Daniel@0 5 % drawn from three spherical Gaussian distributions with priors 0.3,
Daniel@0 6 % 0.5 and 0.2; centres (2, 3.5), (0, 0) and (0,2); and standard
Daniel@0 7 % deviations 0.2, 0.5 and 1.0. The first figure contains a scatter plot
Daniel@0 8 % of the data. The data is the same as in DEMGMM1.
Daniel@0 9 %
Daniel@0 10 % A cluster model with three components is trained using the batch K-
Daniel@0 11 % means algorithm. The matrix of centres is printed after training. The
Daniel@0 12 % second figure shows the data labelled with a colour derived from the
Daniel@0 13 % corresponding cluster
Daniel@0 14 %
Daniel@0 15 % See also
Daniel@0 16 % DEM2DDAT, DEMGMM1, KNN1, KMEANS
Daniel@0 17 %
Daniel@0 18
Daniel@0 19 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 20
Daniel@0 21 % Generate the data, fixing seeds for reproducible results
Daniel@0 22 ndata = 250;
Daniel@0 23 randn('state', 42);
Daniel@0 24 rand('state', 42);
Daniel@0 25 data = dem2ddat(ndata);
Daniel@0 26
Daniel@0 27 % Randomise data order
Daniel@0 28 data = data(randperm(ndata),:);
Daniel@0 29
Daniel@0 30 clc
Daniel@0 31 disp('This demonstration illustrates the use of a cluster model to')
Daniel@0 32 disp('find centres that reflect the distribution of data points.')
Daniel@0 33 disp('We begin by generating the data from a mixture of three Gaussians')
Daniel@0 34 disp('in two-dimensional space and plotting it.')
Daniel@0 35 disp(' ')
Daniel@0 36 disp('Press any key to continue.')
Daniel@0 37 pause
Daniel@0 38
Daniel@0 39 fh1 = figure;
Daniel@0 40 plot(data(:, 1), data(:, 2), 'o')
Daniel@0 41 set(gca, 'Box', 'on')
Daniel@0 42 title('Data')
Daniel@0 43
Daniel@0 44 % Set up cluster model
Daniel@0 45 ncentres = 3;
Daniel@0 46 centres = zeros(ncentres, 2);
Daniel@0 47
Daniel@0 48 % Set up vector of options for kmeans trainer
Daniel@0 49 options = foptions;
Daniel@0 50 options(1) = 1; % Prints out error values.
Daniel@0 51 options(5) = 1;
Daniel@0 52 options(14) = 10; % Number of iterations.
Daniel@0 53
Daniel@0 54 clc
Daniel@0 55 disp('The model is chosen to have three centres, which are initialised')
Daniel@0 56 disp('at randomly selected data points. We now train the model using')
Daniel@0 57 disp('the batch K-means algorithm with a maximum of 10 iterations and')
Daniel@0 58 disp('stopping tolerance of 1e-4.')
Daniel@0 59 disp(' ')
Daniel@0 60 disp('Press any key to continue.')
Daniel@0 61 pause
Daniel@0 62
Daniel@0 63 % Train the centres from the data
Daniel@0 64 [centres, options, post] = kmeans(centres, data, options);
Daniel@0 65
Daniel@0 66 % Print out model
Daniel@0 67 disp(' ')
Daniel@0 68 disp('Note that training has terminated before 10 iterations as there')
Daniel@0 69 disp('has been no change in the centres or error function.')
Daniel@0 70 disp(' ')
Daniel@0 71 disp('The trained model has centres:')
Daniel@0 72 disp(centres);
Daniel@0 73 disp('Press any key to continue.')
Daniel@0 74 pause
Daniel@0 75
Daniel@0 76 clc
Daniel@0 77 disp('We now plot each data point coloured according to its classification')
Daniel@0 78 disp('given by the nearest cluster centre. The cluster centres are denoted')
Daniel@0 79 disp('by black crosses.')
Daniel@0 80
Daniel@0 81 % Plot the result
Daniel@0 82 fh2 = figure;
Daniel@0 83
Daniel@0 84 hold on
Daniel@0 85 colours = ['b.'; 'r.'; 'g.'];
Daniel@0 86
Daniel@0 87 [tempi, tempj] = find(post);
Daniel@0 88 hold on
Daniel@0 89 for i = 1:3
Daniel@0 90 % Select data points closest to ith centre
Daniel@0 91 thisX = data(tempi(tempj == i), 1);
Daniel@0 92 thisY = data(tempi(tempj == i), 2);
Daniel@0 93 hp(i) = plot(thisX, thisY, colours(i,:));
Daniel@0 94 set(hp(i), 'MarkerSize', 12);
Daniel@0 95 end
Daniel@0 96 set(gca, 'Box', 'on')
Daniel@0 97 legend('Class 1', 'Class 2', 'Class 3', 2)
Daniel@0 98 hold on
Daniel@0 99 plot(centres(:, 1), centres(:,2), 'k+', 'LineWidth', 2, ...
Daniel@0 100 'MarkerSize', 8)
Daniel@0 101 title('Centres and data labels')
Daniel@0 102 hold off
Daniel@0 103
Daniel@0 104 disp(' ')
Daniel@0 105 disp('Press any key to end.')
Daniel@0 106 pause
Daniel@0 107
Daniel@0 108 close(fh1);
Daniel@0 109 close(fh2);
Daniel@0 110 clear all;
Daniel@0 111