-
Notifications
You must be signed in to change notification settings - Fork 24
/
kmeans_test.m
45 lines (35 loc) · 1.16 KB
/
kmeans_test.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
%% (C) Copyright 2012. All rights reserved. Sotiris L Karavarsamis.
% Contact author at sokar@aiia.csd.auth.gr
%
% This is an implementation of the k-means algorithm straight from the
% pseudocode description based on the book 'Introduction to Information
% Retrieval' by Manning, Schutze, Raghavan.
close all;
for i=1:10
% clear workspace
clear all;
% set algorithm parameters
TOL = 0.0004;
ITER = 30;
kappa = 4;
% generate random data
X = [1000*randn(1000,2) + 1000; 2000*randn(1000,2) + 5000];
% run k-Means on random data
tic;
[C, I, iter] = myKmeans(X, kappa, ITER, TOL);
toc
% show number of iteration taken by k-means
disp(['k-means instance took ' int2str(iter) ' iterations to complete']);
% available colos for the points in the resulting clustering plot
colors = {'red', 'green', 'blue', 'black'};
% show plot of clustering
figure;
for i=1:kappa
hold on, plot(X(find(I == i), 1), X(find(I == i), 2), '.', 'color', colors{i});
end
% wait key
pause;
end
% pause and close all windows
pause;
close all;