-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
40 lines (40 loc) · 1.31 KB
/
main.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
clear; clc; close all;
prompt='Please enter a positve integer K value \n';
k=input(prompt);
max_iter=1000;
fprintf('Loading and Visualizing Data ...\n');
load('data','X');
assert(isempty(X)==0 && size(X,2)==2,'Data must be a N x 2 matrix where each row contains X and Y coordinate');
assert(isint(k) && k>=1 && k<=size(X,1),'Please make sure that the value of k is a positive integer less than or equal to the number of data points');
figure;
hold on;
scatter(X(:,1),X(:,2),200,'.');
fprintf('Program paused. Press enter to continue.\n');
pause;
fprintf('Generating Optimal K-Means Locations and Plotting Locations...\n');
Means=GenRandom(X,k);
pres=zeros(size(X,1),1);
res=ones(size(X,1),1);
for i=1:max_iter
pres=res;
[res,New_Means]=K_Means(X,Means,k);
if isequal(New_Means,Means)
break
end
Means=New_Means;
end
plot(Means(:,1),Means(:,2),'rx','MarkerSize',50);
fprintf('Program paused. Press enter to continue.\n');
pause;
fprintf('Plotting Data with each cluster having unique colour...\n');
hold off;
figure;
hold on;
for i=1:k
scatter(X(res==i,1),X(res==i,2),50,rand(1,3),'filled');
end
CP_values=compactness(X,Means,res,k);
Compactness_Value=sum(CP_values)/length(CP_values)
Separation=separation(X,Means,res,k)
Davies_Bouldin_Index=DB_Index(X,Means,res,k)
Dunn_Validity_Index=DV_Index(X,Means,res,k)