comparison toolboxes/MIRtoolbox1.3.2/somtoolbox/db_index.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [t,r] = db_index(D, cl, C, p, q)
2
3 % DB_INDEX Davies-Bouldin clustering evaluation index.
4 %
5 % [t,r] = db_index(D, cl, C, p, q)
6 %
7 % Input and output arguments ([]'s are optional):
8 % D (matrix) data (n x dim)
9 % (struct) map or data struct
10 % cl (vector) cluster numbers corresponding to data samples (n x 1)
11 % [C] (matrix) prototype vectors (c x dim) (default = cluster means)
12 % [p] (scalar) norm used in the computation (default == 2)
13 % [q] (scalar) moment used to calculate cluster dispersions (default = 2)
14 %
15 % t (scalar) Davies-Bouldin index for the clustering (=mean(r))
16 % r (vector) maximum DB index for each cluster (size c x 1)
17 %
18 % See also KMEANS, KMEANS_CLUSTERS, SOM_GAPINDEX.
19
20 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21 %% input arguments
22
23 if isstruct(D),
24 switch D.type,
25 case 'som_map', D = D.codebook;
26 case 'som_data', D = D.data;
27 end
28 end
29
30 % cluster centroids
31 [l dim] = size(D);
32 u = unique(cl);
33 c = length(u);
34 if nargin <3,
35 C = zeros(c,dim);
36 for i=1:c,
37 me = nanstats(D(find(cl==u(i)),:));
38 C(i,:) = me';
39 end
40 end
41
42 u2i = zeros(max(u),1); u2i(u) = 1:c;
43 D = som_fillnans(D,C,u2i(cl)); % replace NaN's with cluster centroid values
44
45 if nargin <4, p = 2; end % euclidian distance between cluster centers
46 if nargin <5, q = 2; end % dispersion = standard deviation
47
48 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %% action
50
51 % dispersion in each cluster
52 for i = 1:c
53 ind = find(cl==u(i)); % points in this cluster
54 l = length(ind);
55 if l > 0
56 S(i) = (mean(sqrt(sum((D(ind,:) - ones(l,1) * C(i,:)).^2,2)).^q))^(1/q);
57 else
58 S(i) = NaN;
59 end
60 end
61
62 % distances between clusters
63 %for i = 1:c
64 % for j = i+1:c
65 % M(i,j) = sum(abs(C(i,:) - C(j,:)).^p)^(1/p);
66 % end
67 %end
68 M = som_mdist(C,p);
69
70 % Davies-Bouldin index
71 R = NaN * zeros(c);
72 r = NaN * zeros(c,1);
73 for i = 1:c
74 for j = i+1:c
75 R(i,j) = (S(i) + S(j))/M(i,j);
76 end
77 r(i) = max(R(i,:));
78 end
79
80 t = mean(r(isfinite(r)));
81
82 return;
83