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