| Description of slkmeans |
slkmeans
PURPOSE 
SLKMEANS Performs K-Means Clustering on samples
SYNOPSIS 
function [means, labels] = slkmeans(X, varargin)
DESCRIPTION 
CROSS-REFERENCE INFORMATION 
This function calls:
- annsearch ANNSEARCH Approximate Nearest Neighbor Search
- slkmeansex SLKMEANSEX Performs Generalized K-means
- slmetric_pw SLMETRIC_PW Compute the metric between column vectors pairwisely
- slmean SLMEAN Compute the mean vector of samples
- slignorevars SLIGNOREVARS Ignores the input variables
- sllabelinds SLLABELINDS Extract indices corresponding to specified labels
- slparseprops SLPARSEPROPS Parses input parameters
This function is called by:
SUBFUNCTIONS 
- function centers = kmeans_est(centers, X, K, weights, labels)
- function labels = kmeans_classify(centers, X, n, fh_classify)
- function centers = kmeans_anneal(centers, inds_discard)
- function L = classify_normal(centers, data)
- function L = classify_samplewise(centers, data)
- function L = classify_ann(centers, data)
SOURCE CODE 
0001 function [means, labels] = slkmeans(X, 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 if ndims(X) ~= 2
0059 error('sltoolbox:invaliddims', 'X should be a 2D matrix');
0060 end
0061
0062 opts.K = 3;
0063 opts.init_means = [];
0064 opts.clsfunc = 'normal';
0065 opts.maxiter = 100;
0066 opts.annthres = 0;
0067 opts.weights = [];
0068 opts.verbose = true;
0069 opts = slparseprops(opts, varargin{:});
0070
0071 n = size(X, 2);
0072
0073 if ischar(opts.clsfunc)
0074 switch opts.clsfunc
0075 case 'normal'
0076 fh_classify = @classify_normal;
0077 case 'samplewise'
0078 fh_classify = @classify_samplewise;
0079 case 'ann'
0080 fh_classify = @classify_ann;
0081 otherwise
0082 error('sltoolbox:invalidarg', ...
0083 'Invalid clsfunc option %s', opts.clsfunc);
0084 end
0085 elseif isa(opts.clsfunc, 'function_handle')
0086 fh_classify = opts.clsfunc;
0087 else
0088 error('sltoolbox:invalidarg', ...
0089 'clsfunc can be either a string or a function handle');
0090 end
0091
0092
0093
0094
0095 estfunctor = {@kmeans_est};
0096 clsfunctor = {@kmeans_classify, fh_classify};
0097 annfunc = @kmeans_anneal;
0098
0099 [means, labels] = slkmeansex(X, n, estfunctor, clsfunctor, ...
0100 'K', opts.K, ...
0101 'init_centers', opts.init_means, ...
0102 'maxiter', opts.maxiter, ...
0103 'annthres', opts.annthres, ...
0104 'annfunc', annfunc, ...
0105 'weights', opts.weights, ...
0106 'verbose', opts.verbose);
0107
0108
0109
0110
0111
0112 function centers = kmeans_est(centers, X, K, weights, labels)
0113
0114 d = size(X, 1);
0115 if isempty(centers)
0116 centers = zeros(d, K);
0117 end
0118
0119 Inds = sllabelinds(labels, 1:K);
0120 for i = 1 : K
0121 si = Inds{i};
0122
0123 if ~isempty(si)
0124 curX = X(:, si);
0125 if isempty(weights)
0126 curw = [];
0127 else
0128 curw = weights(si);
0129 end
0130 centers(:, i) = slmean(curX, curw);
0131 end
0132 end
0133
0134
0135 function labels = kmeans_classify(centers, X, n, fh_classify)
0136
0137 slignorevars(n);
0138 labels = fh_classify(centers, X);
0139
0140
0141 function centers = kmeans_anneal(centers, inds_discard)
0142
0143 centers(:, inds_discard) = [];
0144
0145
0146
0147
0148
0149
0150 function L = classify_normal(centers, data)
0151
0152 dists = slmetric_pw(centers, data, 'eucdist');
0153 [md, L] = min(dists, [], 1);
0154 slignorevars(md);
0155
0156 function L = classify_samplewise(centers, data)
0157
0158 n = size(data, 2);
0159 L = zeros(1, n);
0160 for i = 1 : n
0161 curdists = slmetric_pw(centers, data(:, i), 'eucdist');
0162 [md, p] = min(curdists);
0163 L(i) = p;
0164 end
0165 slignorevars(md);
0166
0167 function L = classify_ann(centers, data)
0168
0169 L = annsearch(centers, data, 1);
0170 L = L(:)';
0171
0172
0173
0174
0175
0176
0177
0178
0179
Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003
|
|