| Description of slkmeansex |
slkmeansex
PURPOSE 
SLKMEANSEX Performs Generalized K-means
SYNOPSIS 
function [centers, labels, info] = slkmeansex(X, n, estfunctor, clsfunctor, varargin)
DESCRIPTION 
CROSS-REFERENCE INFORMATION 
This function calls:
This function is called by:
- slkmeans SLKMEANS Performs K-Means Clustering on samples
SUBFUNCTIONS 
- function models = kmeansex_est(models, data, labels, estfunctor, opts)
- function labels = kmeansex_eval(models, data, labels, clsfunctor)
- function isconverged = kmeansex_cmp(models_prev, models, labels_prev, labels)
SOURCE CODE 
0001 function [centers, labels, info] = slkmeansex(X, n, estfunctor, clsfunctor, varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067 if nargin < 4
0068 raise_lackinput('slkmeansex', 4);
0069 end
0070
0071 opts.K = 3;
0072 opts.init_centers = [];
0073 opts.maxiter = 100;
0074 opts.annthres = 0;
0075 opts.annfunc = [];
0076 opts.weights = [];
0077 opts.verbose = true;
0078 opts = slparseprops(opts, varargin{:});
0079
0080 if opts.K > n
0081 error('sltoolbox:rterror', ...
0082 'The initial K is larger than the number of samples');
0083 end
0084
0085 if opts.annthres > 0
0086 if isempty(opts.annfunc)
0087 error('sltoolbox:invalidarg', ...
0088 'You should specify annfunc when annthres > 0');
0089 end
0090 end
0091
0092 w = opts.weights;
0093 if ~isempty(w)
0094 if ~isequal(w, [1 n])
0095 error('sltoolbox:sizmismatch', ...
0096 'The weights should be a 1 x n row vector');
0097 end
0098 end
0099
0100
0101
0102
0103 slsharedisp_attach('slkmeansex', 'show', opts.verbose);
0104
0105 slsharedisp('Intialize K-Means');
0106
0107 if isempty(opts.init_centers)
0108 initcinds = randsample(n, opts.K);
0109 labels = zeros(1, n);
0110 labels(initcinds) = 1:opts.K;
0111
0112 K = opts.K;
0113 centers = slevalfunctor(estfunctor, [], X, K, w, labels);
0114 else
0115 K = opts.K;
0116 centers = opts.init_centers;
0117 end
0118
0119 slsharedisp_incindent;
0120 slsharedisp('initial K = %d', K);
0121 slsharedisp_decindent;
0122
0123 labels = slevalfunctor(clsfunctor, centers, X, n);
0124
0125
0126
0127
0128 slsharedisp('Update K-Means');
0129 slsharedisp_incindent;
0130
0131 km_estfunctor = {@kmeansex_est, estfunctor, opts};
0132 km_evalfunctor = {@kmeansex_eval, clsfunctor};
0133 km_cmpfunctor = {@kmeansex_cmp};
0134
0135 models = {centers, K};
0136 data = {X, n, w};
0137 [models, labels, info] = slreevallearn(models, labels, data, ...
0138 km_estfunctor, km_evalfunctor, km_cmpfunctor, ...
0139 'iter', {'maxiter', opts.maxiter, 'titlebreak', false}, 'isrecorded', false);
0140
0141 centers = models{1};
0142
0143 slsharedisp_decindent;
0144 slsharedisp_detach;
0145
0146
0147
0148
0149
0150
0151 function models = kmeansex_est(models, data, labels, estfunctor, opts)
0152
0153 X = data{1};
0154 w = data{3};
0155 centers = models{1};
0156 K = models{2};
0157
0158 if ~isempty(centers) && opts.annthres > 0
0159 if isempty(w)
0160 w = ones(1, length(labels));
0161 end
0162 cw = sllabeledsum(w, labels, 1:K);
0163 wthres = opts.annthres * sum(cw) / K;
0164 if any(cw < wthres)
0165 inds_ann = find(cw < wthres);
0166 centers = feval(opts.annfunc, centers, inds_ann);
0167 K = K - length(inds_ann);
0168
0169 models = {centers, K};
0170 return;
0171 end
0172 end
0173
0174 centers = slevalfunctor(estfunctor, centers, X, K, w, labels);
0175 models = {centers, K};
0176
0177
0178 function labels = kmeansex_eval(models, data, labels, clsfunctor)
0179
0180 X = data{1};
0181 n = data{2};
0182 centers = models{1};
0183
0184 slignorevars(labels);
0185
0186 labels = slevalfunctor(clsfunctor, centers, X, n);
0187
0188
0189 function isconverged = kmeansex_cmp(models_prev, models, labels_prev, labels)
0190
0191 K_prev = models_prev{2};
0192 K = models{2};
0193 n = length(labels);
0194
0195 slsharedisp_attach('kmeansex_cmp');
0196
0197 isconverged = false;
0198 if K == K_prev
0199 nchanged = sum(labels ~= labels_prev);
0200 slsharedisp('K = %d: %d / %d changed', K, nchanged, n);
0201
0202 if nchanged == 0
0203 isconverged = true;
0204 end
0205 else
0206 slsharedisp('K = %d ==> %d', K_prev, K);
0207 end
0208
0209 slsharedisp_detach();
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003
|
|