Browse code

starting som prediction fine-tuned class-performance visualisation

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@112 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on21/01/2009 16:34:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,72 @@
1
+function [t,r,Cd,S] = som_gapindex(sM, base, between)
2
+ 
3
+% SOM_GAPINDEX Gap clustering evaluation index.
4
+%
5
+% [t,r] = som_gapindex(sM, base, [between])
6
+%
7
+%  Input and output arguments ([]'s are optional):  
8
+%    sM        (struct) map struct
9
+%    base      (vector) clusters indeces for each map unit, map units
10
+%                       with index<=0 or NaN are not taken into account
11
+%    [between] (vector) indices of prototypes which are "between" clusters:
12
+%                       the associated distances are doubled
13
+% 
14
+%    t         (scalar) Gap index index for the clustering (=mean(r))
15
+%    r         (vector) maximum Gap index for each cluster (size max(base) x 1)    
16
+% 
17
+% See also  KMEANS, KMEANS_CLUSTERS, SOM_GAPINDEX.
18
+
19
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
+
21
+if nargin<3, between = find(isnan(base)); end
22
+
23
+nc = max(base); 
24
+cinds = cell(nc,1); 
25
+for i=1:nc, cinds{i} = find(base==i); end
26
+ 
27
+% distances between neighboring prototypes
28
+Ne = som_neighbors(sM,'N1'); 
29
+Md = som_mdist(sM.codebook,2,[],Ne);
30
+Md(Ne==0) = NaN;
31
+
32
+Md(between,:) = Md(between,:)*2;
33
+Md(:,between) = Md(:,between)*2;
34
+Md(between,between) = Md(between,between)/2;
35
+ 
36
+% dispersion in each cluster 
37
+S = zeros(nc,1);
38
+for i=1:nc, 
39
+    inds = setdiff(cinds{i},between);    
40
+    if any(inds), 
41
+        indist = Md(inds,inds); 
42
+        for j=1:size(indist,1), indist(j,j) = NaN; end
43
+        indist = indist(isfinite(indist(:))); 
44
+        if any(indist), S(i) = mean(indist); end
45
+    end
46
+end 
47
+ 
48
+% distances between clusters
49
+Cd = zeros(nc,nc) + NaN;
50
+for i=1:nc,
51
+    inds1 = cinds{i}; 
52
+    for j=1:nc, 
53
+        inds2 = cinds{j}; 
54
+	od = Md(inds1,inds2); 
55
+	od = od(isfinite(od(:)));
56
+	if any(od), Cd(i,j) = mean(od(:)); end        
57
+    end    
58
+end
59
+
60
+% Gap index
61
+R = NaN * zeros(nc);
62
+for i = 1:nc
63
+  for j = i+1:nc
64
+    R(i,j) = (S(i) + S(j))/Cd(i,j);
65
+    R(j,i) = R(i,j); 
66
+  end
67
+end
68
+r = max(R,[],2);
69
+ 
70
+t = mean(r(isfinite(r)));
71
+ 
72
+return;