-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgg_kmeans.m
47 lines (36 loc) · 1.08 KB
/
vgg_kmeans.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function [CX, sse] = vgg_kmeans(X, nclus, varargin)
% VGG_KMEANS initialize K-means clustering
% [CX, sse] = vgg_kmeans(X, nclus, optname, optval, ...)
%
% - X: input points (one per column)
% - nclus: number of clusters
% - opts (defaults):
% maxiters (inf): maxmimum number of iterations
% mindelta (eps): minimum change in SSE per iteration
% verbose (1): 1=print progress
%
% - CX: cluster centers
% - sse: SSE
% Author: Mark Everingham <[email protected]>
% Date: 13 Jan 03
opts = struct('maxiters', inf, 'mindelta', eps, 'verbose', 1);
if nargin > 2
opts=vgg_argparse(opts,varargin);
end
perm=randperm(size(X,2));
CX=X(:,perm(1:nclus));
sse0 = inf;
iter = 0;
while iter < opts.maxiters
tic;
[CX, sse] = vgg_kmiter(X, CX);
t=toc;
if opts.verbose
fprintf('iter %d: sse = %g (%g secs)\n', iter, sse, t)
end
if sse0-sse < opts.mindelta
break
end
sse0=sse;
iter=iter+1;
end