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