Daniel@0: function [t,r] = db_index(D, cl, C, p, q) Daniel@0: Daniel@0: % DB_INDEX Davies-Bouldin clustering evaluation index. Daniel@0: % Daniel@0: % [t,r] = db_index(D, cl, C, p, q) Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % D (matrix) data (n x dim) Daniel@0: % (struct) map or data struct Daniel@0: % cl (vector) cluster numbers corresponding to data samples (n x 1) Daniel@0: % [C] (matrix) prototype vectors (c x dim) (default = cluster means) Daniel@0: % [p] (scalar) norm used in the computation (default == 2) Daniel@0: % [q] (scalar) moment used to calculate cluster dispersions (default = 2) Daniel@0: % Daniel@0: % t (scalar) Davies-Bouldin index for the clustering (=mean(r)) Daniel@0: % r (vector) maximum DB index for each cluster (size c x 1) Daniel@0: % Daniel@0: % See also KMEANS, KMEANS_CLUSTERS, SOM_GAPINDEX. Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% input arguments Daniel@0: Daniel@0: if isstruct(D), Daniel@0: switch D.type, Daniel@0: case 'som_map', D = D.codebook; Daniel@0: case 'som_data', D = D.data; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % cluster centroids Daniel@0: [l dim] = size(D); Daniel@0: u = unique(cl); Daniel@0: c = length(u); Daniel@0: if nargin <3, Daniel@0: C = zeros(c,dim); Daniel@0: for i=1:c, Daniel@0: me = nanstats(D(find(cl==u(i)),:)); Daniel@0: C(i,:) = me'; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: u2i = zeros(max(u),1); u2i(u) = 1:c; Daniel@0: D = som_fillnans(D,C,u2i(cl)); % replace NaN's with cluster centroid values Daniel@0: Daniel@0: if nargin <4, p = 2; end % euclidian distance between cluster centers Daniel@0: if nargin <5, q = 2; end % dispersion = standard deviation Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% action Daniel@0: Daniel@0: % dispersion in each cluster Daniel@0: for i = 1:c Daniel@0: ind = find(cl==u(i)); % points in this cluster Daniel@0: l = length(ind); Daniel@0: if l > 0 Daniel@0: S(i) = (mean(sqrt(sum((D(ind,:) - ones(l,1) * C(i,:)).^2,2)).^q))^(1/q); Daniel@0: else Daniel@0: S(i) = NaN; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % distances between clusters Daniel@0: %for i = 1:c Daniel@0: % for j = i+1:c Daniel@0: % M(i,j) = sum(abs(C(i,:) - C(j,:)).^p)^(1/p); Daniel@0: % end Daniel@0: %end Daniel@0: M = som_mdist(C,p); Daniel@0: Daniel@0: % Davies-Bouldin index Daniel@0: R = NaN * zeros(c); Daniel@0: r = NaN * zeros(c,1); Daniel@0: for i = 1:c Daniel@0: for j = i+1:c Daniel@0: R(i,j) = (S(i) + S(j))/M(i,j); Daniel@0: end Daniel@0: r(i) = max(R(i,:)); Daniel@0: end Daniel@0: Daniel@0: t = mean(r(isfinite(r))); Daniel@0: Daniel@0: return; Daniel@0: