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