annotate core/tools/machine_learning/weighted_kmeans.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 function [centres, cweights, post, errlog, options] = weighted_kmeans(centres, data, weights, options)
Daniel@0 2 %[centres, cweights, post, errlog, options] = weighted_kmeans(centres,data, weights, options)
Daniel@0 3 %
Daniel@0 4 % weighted_kmeans Trains a k means cluster model on weighted input vectors
Daniel@0 5 %
Daniel@0 6 % Adapted from the Netlab Toolbox by Daniel Wolff,
Daniel@0 7 % This function takes a WEIGHTS vector, containing weights for the
Daniel@0 8 % different data points. This can be used for training with varying
Daniel@0 9 % discretisation intervals.
Daniel@0 10 %
Daniel@0 11 % Description
Daniel@0 12 % CENTRES = weighted_kmeans(NCENTRES, DATA, WEIGHTS, OPTIONS) or
Daniel@0 13 % CENTRES = weighted_kmeans(CENTRES, DATA, WEIGHTS, OPTIONS) uses the batch K-means
Daniel@0 14 % algorithm to set the centres of a cluster model. The matrix DATA
Daniel@0 15 % represents the data which is being clustered, with each row
Daniel@0 16 % corresponding to a vector. The sum of squares error function is used.
Daniel@0 17 % The point at which a local minimum is achieved is returned as
Daniel@0 18 % CENTRES. The error value at that point is returned in OPTIONS(8).
Daniel@0 19 %
Daniel@0 20 %
Daniel@0 21 % POST and ERRLOG
Daniel@0 22 % also return the cluster number (in a one-of-N encoding) for each
Daniel@0 23 % data point in POST and a log of the error values after each cycle in
Daniel@0 24 % ERRLOG. The optional parameters have the following
Daniel@0 25 % interpretations.
Daniel@0 26 %
Daniel@0 27 % OPTIONS(1) is set to 1 to display error values; also logs error
Daniel@0 28 % values in the return argument ERRLOG. If OPTIONS(1) is set to 0, then
Daniel@0 29 % only warning messages are displayed. If OPTIONS(1) is -1, then
Daniel@0 30 % nothing is displayed.
Daniel@0 31 %
Daniel@0 32 % OPTIONS(2) is a measure of the absolute precision required for the
Daniel@0 33 % value of CENTRES at the solution. If the absolute difference between
Daniel@0 34 % the values of CENTRES between two successive steps is less than
Daniel@0 35 % OPTIONS(2), then this condition is satisfied.
Daniel@0 36 %
Daniel@0 37 % OPTIONS(3) is a measure of the precision required of the error
Daniel@0 38 % function at the solution. If the absolute difference between the
Daniel@0 39 % error functions between two successive steps is less than OPTIONS(3),
Daniel@0 40 % then this condition is satisfied. Both this and the previous
Daniel@0 41 % condition must be satisfied for termination.
Daniel@0 42 %
Daniel@0 43 % OPTIONS(14) is the maximum number of iterations; default 100.
Daniel@0 44 %
Daniel@0 45 % See also
Daniel@0 46 % GMMINIT, GMMEM
Daniel@0 47 %
Daniel@0 48
Daniel@0 49 % Copyright (c) Ian T Nabney (1996-2001)
Daniel@0 50
Daniel@0 51 [ndata, data_dim] = size(data);
Daniel@0 52 [ncentres, dim] = size(centres);
Daniel@0 53
Daniel@0 54 if dim ~= data_dim
Daniel@0 55 if dim == 1 && ncentres == 1 && centres > 1
Daniel@0 56
Daniel@0 57 if ndata == numel(weights)
Daniel@0 58
Daniel@0 59 % ---
Daniel@0 60 % allow for number of centres specification
Daniel@0 61 % ---
Daniel@0 62 dim = data_dim;
Daniel@0 63 ncentres = centres;
Daniel@0 64
Daniel@0 65 options(5) = 1;
Daniel@0 66 else
Daniel@0 67 error('Data dimension does not match number of weights')
Daniel@0 68 end
Daniel@0 69
Daniel@0 70 else
Daniel@0 71 error('Data dimension does not match dimension of centres')
Daniel@0 72 end
Daniel@0 73 end
Daniel@0 74
Daniel@0 75 if (ncentres > ndata)
Daniel@0 76 error('More centres than data')
Daniel@0 77 end
Daniel@0 78
Daniel@0 79 % Sort out the options
Daniel@0 80 if (options(14))
Daniel@0 81 niters = options(14);
Daniel@0 82 else
Daniel@0 83 niters = 100;
Daniel@0 84 end
Daniel@0 85
Daniel@0 86 store = 0;
Daniel@0 87 if (nargout > 3)
Daniel@0 88 store = 1;
Daniel@0 89 errlog = zeros(1, niters);
Daniel@0 90 end
Daniel@0 91
Daniel@0 92 % Check if centres and posteriors need to be initialised from data
Daniel@0 93 if (options(5) == 1)
Daniel@0 94 % Do the initialisation
Daniel@0 95 perm = randperm(ndata);
Daniel@0 96 perm = perm(1:ncentres);
Daniel@0 97
Daniel@0 98 % Assign first ncentres (permuted) data points as centres
Daniel@0 99 centres = data(perm, :);
Daniel@0 100 end
Daniel@0 101 % Matrix to make unit vectors easy to construct
Daniel@0 102 id = eye(ncentres);
Daniel@0 103
Daniel@0 104 % save accumulated weight for a center
Daniel@0 105 cweights = zeros(ncentres, 1);
Daniel@0 106
Daniel@0 107 % Main loop of algorithm
Daniel@0 108 for n = 1:niters
Daniel@0 109
Daniel@0 110 % Save old centres to check for termination
Daniel@0 111 old_centres = centres;
Daniel@0 112
Daniel@0 113 % Calculate posteriors based on existing centres
Daniel@0 114 d2 = dist2(data, centres);
Daniel@0 115 % Assign each point to nearest centre
Daniel@0 116 [minvals, index] = min(d2', [], 1);
Daniel@0 117 post = logical(id(index,:));
Daniel@0 118
Daniel@0 119 % num_points = sum(post, 1);
Daniel@0 120 % Adjust the centres based on new posteriors
Daniel@0 121 for j = 1:ncentres
Daniel@0 122 if (sum(weights(post(:,j))) > 0)
Daniel@0 123 % ---
Daniel@0 124 % NOTE: this is edited to include the weights.
Daniel@0 125 % Instead of summing the vectors directly, the vectors are weighted
Daniel@0 126 % and then the result is divided by the sum of the weights instead
Daniel@0 127 % of the number of vectors for this class
Daniel@0 128 % ---
Daniel@0 129 cweights(j) = sum(weights(post(:,j)));
Daniel@0 130
Daniel@0 131 centres(j,:) = sum(diag(weights(post(:,j))) * data(post(:,j),:), 1)...
Daniel@0 132 /cweights(j);
Daniel@0 133 end
Daniel@0 134 end
Daniel@0 135
Daniel@0 136 % Error value is total squared distance from cluster centres
Daniel@0 137 % edit: weighted by the vectors weight
Daniel@0 138 e = sum(minvals .* weights);
Daniel@0 139 if store
Daniel@0 140 errlog(n) = e;
Daniel@0 141 end
Daniel@0 142 if options(1) > 0
Daniel@0 143 fprintf(1, 'Cycle %4d Error %11.6f\n', n, e);
Daniel@0 144 end
Daniel@0 145
Daniel@0 146 if n > 1
Daniel@0 147 % Test for termination
Daniel@0 148 if max(max(abs(centres - old_centres))) < options(2) & ...
Daniel@0 149 abs(old_e - e) < options(3)
Daniel@0 150 options(8) = e;
Daniel@0 151 return;
Daniel@0 152 end
Daniel@0 153 end
Daniel@0 154 old_e = e;
Daniel@0 155 end
Daniel@0 156
Daniel@0 157 % If we get here, then we haven't terminated in the given number of
Daniel@0 158 % iterations.
Daniel@0 159 options(8) = e;
Daniel@0 160 if (options(1) >= 0)
Daniel@0 161 disp(maxitmess);
Daniel@0 162 end
Daniel@0 163