Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/netlab3.3/kmeansNetlab.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 function [centres, options, post, errlog] = kmeans(centres, data, options) | |
2 %KMEANS Trains a k means cluster model. | |
3 % | |
4 % Description | |
5 % CENTRES = KMEANS(CENTRES, DATA, OPTIONS) uses the batch K-means | |
6 % algorithm to set the centres of a cluster model. The matrix DATA | |
7 % represents the data which is being clustered, with each row | |
8 % corresponding to a vector. The sum of squares error function is used. | |
9 % The point at which a local minimum is achieved is returned as | |
10 % CENTRES. The error value at that point is returned in OPTIONS(8). | |
11 % | |
12 % [CENTRES, OPTIONS, POST, ERRLOG] = KMEANS(CENTRES, DATA, OPTIONS) | |
13 % also returns the cluster number (in a one-of-N encoding) for each | |
14 % data point in POST and a log of the error values after each cycle in | |
15 % ERRLOG. The optional parameters have the following | |
16 % interpretations. | |
17 % | |
18 % OPTIONS(1) is set to 1 to display error values; also logs error | |
19 % values in the return argument ERRLOG. If OPTIONS(1) is set to 0, then | |
20 % only warning messages are displayed. If OPTIONS(1) is -1, then | |
21 % nothing is displayed. | |
22 % | |
23 % OPTIONS(2) is a measure of the absolute precision required for the | |
24 % value of CENTRES at the solution. If the absolute difference between | |
25 % the values of CENTRES between two successive steps is less than | |
26 % OPTIONS(2), then this condition is satisfied. | |
27 % | |
28 % OPTIONS(3) is a measure of the precision required of the error | |
29 % function at the solution. If the absolute difference between the | |
30 % error functions between two successive steps is less than OPTIONS(3), | |
31 % then this condition is satisfied. Both this and the previous | |
32 % condition must be satisfied for termination. | |
33 % | |
34 % OPTIONS(14) is the maximum number of iterations; default 100. | |
35 % | |
36 % See also | |
37 % GMMINIT, GMMEM | |
38 % | |
39 | |
40 % Copyright (c) Ian T Nabney (1996-2001) | |
41 | |
42 [ndata, data_dim] = size(data); | |
43 [ncentres, dim] = size(centres); | |
44 | |
45 if dim ~= data_dim | |
46 error('Data dimension does not match dimension of centres') | |
47 end | |
48 | |
49 if (ncentres > ndata) | |
50 error('More centres than data') | |
51 end | |
52 | |
53 % Sort out the options | |
54 if (options(14)) | |
55 niters = options(14); | |
56 else | |
57 niters = 100; | |
58 end | |
59 | |
60 store = 0; | |
61 if (nargout > 3) | |
62 store = 1; | |
63 errlog = zeros(1, niters); | |
64 end | |
65 | |
66 % Check if centres and posteriors need to be initialised from data | |
67 if (options(5) == 1) | |
68 % Do the initialisation | |
69 perm = randperm(ndata); | |
70 perm = perm(1:ncentres); | |
71 | |
72 % Assign first ncentres (permuted) data points as centres | |
73 centres = data(perm, :); | |
74 end | |
75 % Matrix to make unit vectors easy to construct | |
76 id = eye(ncentres); | |
77 | |
78 % Main loop of algorithm | |
79 for n = 1:niters | |
80 | |
81 % Save old centres to check for termination | |
82 old_centres = centres; | |
83 | |
84 % Calculate posteriors based on existing centres | |
85 d2 = dist2(data, centres); | |
86 % Assign each point to nearest centre | |
87 [minvals, index] = min(d2', [], 1); | |
88 post = id(index,:); | |
89 | |
90 num_points = sum(post, 1); | |
91 % Adjust the centres based on new posteriors | |
92 for j = 1:ncentres | |
93 if (num_points(j) > 0) | |
94 centres(j,:) = sum(data(find(post(:,j)),:), 1)/num_points(j); | |
95 end | |
96 end | |
97 | |
98 % Error value is total squared distance from cluster centres | |
99 e = sum(minvals); | |
100 if store | |
101 errlog(n) = e; | |
102 end | |
103 if options(1) > 0 | |
104 fprintf(1, 'Cycle %4d Error %11.6f\n', n, e); | |
105 end | |
106 | |
107 if n > 1 | |
108 % Test for termination | |
109 if max(max(abs(centres - old_centres))) < options(2) & ... | |
110 abs(old_e - e) < options(3) | |
111 options(8) = e; | |
112 return; | |
113 end | |
114 end | |
115 old_e = e; | |
116 end | |
117 | |
118 % If we get here, then we haven't terminated in the given number of | |
119 % iterations. | |
120 options(8) = e; | |
121 if (options(1) >= 0) | |
122 disp(maxitmess); | |
123 end | |
124 |