wolffd@0
|
1 function kmeans_demo()
|
wolffd@0
|
2
|
wolffd@0
|
3 % Generate T points from K=5 1D clusters, and try to recover the cluster
|
wolffd@0
|
4 % centers using k-means.
|
wolffd@0
|
5 % Requires BNT, netlab and the matlab stats toolbox v4.
|
wolffd@0
|
6
|
wolffd@0
|
7 K = 5;
|
wolffd@0
|
8 ndim = 1;
|
wolffd@0
|
9 true_centers = 1:K;
|
wolffd@0
|
10 sigma = 1e-6;
|
wolffd@0
|
11 T = 100;
|
wolffd@0
|
12 % data(t,:) is the t'th data point
|
wolffd@0
|
13 data = zeros(T, ndim);
|
wolffd@0
|
14 % ndx(t) = i means the t'th data point is sample from cluster i
|
wolffd@0
|
15 %ndx = sample_discrete(normalise(ones(1,K)));
|
wolffd@0
|
16 ndx = [1*ones(1,20) 2*ones(1,20) 3*ones(1,20) 4*ones(1,20) 5*ones(1,20)];
|
wolffd@0
|
17 for t=1:T
|
wolffd@0
|
18 data(t) = sample_gaussian(true_centers(ndx(t)), sigma, 1);
|
wolffd@0
|
19 end
|
wolffd@0
|
20 plot(1:T, data, 'x')
|
wolffd@0
|
21
|
wolffd@0
|
22
|
wolffd@0
|
23
|
wolffd@0
|
24 % set the centers randomly from Gauss(0)
|
wolffd@0
|
25 mix = gmm(ndim, K, 'spherical');
|
wolffd@0
|
26 h = plot_centers_as_lines(mix, [], T);
|
wolffd@0
|
27
|
wolffd@0
|
28 if 0
|
wolffd@0
|
29 % Place initial centers at K data points chosen at random, but add some noise
|
wolffd@0
|
30 choose_ndx = randperm(T);
|
wolffd@0
|
31 choose_ndx = choose_ndx(1:K);
|
wolffd@0
|
32 init_centers = data(choose_ndx) + sample_gaussian(0, 0.1, K);
|
wolffd@0
|
33 mix.centres = init_centers;
|
wolffd@0
|
34 h = plot_centers_as_lines(mix, h, T);
|
wolffd@0
|
35 end
|
wolffd@0
|
36
|
wolffd@0
|
37 if 0
|
wolffd@0
|
38 % update centers using netlab k-means
|
wolffd@0
|
39 options = foptions;
|
wolffd@0
|
40 niter = 10;
|
wolffd@0
|
41 options(14) = niter;
|
wolffd@0
|
42 mix = gmminit(mix, data, options);
|
wolffd@0
|
43 h = plot_centers_as_lines(mix, h, T);
|
wolffd@0
|
44 end
|
wolffd@0
|
45
|
wolffd@0
|
46 % use matlab stats toolbox k-means with multiple restarts
|
wolffd@0
|
47 nrestarts = 5;
|
wolffd@0
|
48 [idx, centers] = kmeans(data, K, 'replicates', nrestarts, ...
|
wolffd@0
|
49 'emptyAction', 'singleton', 'display', 'iter');
|
wolffd@0
|
50 mix.centres = centers;
|
wolffd@0
|
51 h = plot_centers_as_lines(mix, h, T);
|
wolffd@0
|
52
|
wolffd@0
|
53 % fine tune with EM; compute covariances of each cluster
|
wolffd@0
|
54 options = foptions;
|
wolffd@0
|
55 niter = 20;
|
wolffd@0
|
56 options(1) = 1; % display cost fn at each iter
|
wolffd@0
|
57 options(14) = niter;
|
wolffd@0
|
58 mix = gmmem(mix, data, options);
|
wolffd@0
|
59 h = plot_centers_as_lines(mix, h, T);
|
wolffd@0
|
60
|
wolffd@0
|
61 %%%%%%%%%
|
wolffd@0
|
62 function h = plot_centers_as_lines(mix, h, T)
|
wolffd@0
|
63
|
wolffd@0
|
64 K = mix.ncentres;
|
wolffd@0
|
65 hold on
|
wolffd@0
|
66 if isempty(h)
|
wolffd@0
|
67 for k=1:K
|
wolffd@0
|
68 h(k)=line([0 T], [mix.centres(k) mix.centres(k)]);
|
wolffd@0
|
69 end
|
wolffd@0
|
70 else
|
wolffd@0
|
71 for k=1:K
|
wolffd@0
|
72 set(h(k), 'xdata', [0 T], 'ydata', [mix.centres(k) mix.centres(k)]);
|
wolffd@0
|
73 end
|
wolffd@0
|
74 end
|
wolffd@0
|
75 hold off
|
wolffd@0
|
76
|