-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdavies.m
47 lines (38 loc) · 1.22 KB
/
davies.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
47
% clusters is a k x n matrix
% clusterI contains the cluster each of the m training examples belongs to
% examples is the m x n matrix of training examples
%
% val is the Davies Bouldin index which should be a measure of "goodness" of
% this particular set of clusters. It should always be less than 1 and lower
% is better.
function [val] = davies(clusters, clusterI, examples)
nclusters = size(clusters, 1);
totDist = zeros(nclusters, 1);
numAss = zeros(nclusters, 1);
m = size(examples, 1);
for i = 1:m
c = clusterI(i);
dist = norm(examples(i,:) - clusters(c,:));
totDist(c) = totDist(c) + dist;
numAss(c) = numAss(c) + 1;
end
avgDist = zeros(nclusters, 1);
for i = 1:nclusters
avgDist(i) = totDist(i) / numAss(i);
end
tot = 0;
for i = 1:nclusters
max = -1;
for j = 1:nclusters
if i == j
break;
end
v = (avgDist(i) + avgDist(j)) * (1.0 / (norm(clusters(i,:) - clusters(j,:))));
if v > max
max = v;
end
end
tot = tot + max;
end
val = tot / nclusters;
end