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,156 @@
1
+function [mqe,tge] = som_quality(sMap, D)
2
+
3
+%SOM_QUALITY Calculate the mean quantization and topographic error.
4
+%
5
+% [qe,te] = som_quality(sMap, D)
6
+%
7
+%  qe = som_quality(sMap,D);
8
+%  [qe,te] = som_quality(sMap,sD);
9
+%
10
+%  Input and output arguments: 
11
+%   sMap     (struct) a map struct
12
+%   D                 the data
13
+%            (struct) a data struct
14
+%            (matrix) a data matrix, size dlen x dim
15
+%
16
+%   qe       (scalar) mean quantization error
17
+%   te       (scalar) topographic error
18
+%
19
+% The issue of SOM quality is a complicated one. Typically two
20
+% evaluation criterias are used: resolution and topology preservation.
21
+% If the dimension of the data set is higher than the dimension of the 
22
+% map grid, these usually become contradictory goals. 
23
+%
24
+% The first value returned by this function measures resolution and the
25
+% second the topology preservation.
26
+%  qe : Average distance between each data vector and its BMU.
27
+%  te : Topographic error, the proportion of all data vectors
28
+%       for which first and second BMUs are not adjacent units.
29
+%
30
+% NOTE: when calculating BMUs of data vectors, the mask of the given 
31
+%       map is used.
32
+%
33
+% For more help, try 'type som_quality' or check out the online documentation.
34
+% See also SOM_BMUS.
35
+
36
+%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37
+%
38
+% som_quality
39
+%
40
+% PURPOSE
41
+%
42
+% Calculates two quality measures for the given map.
43
+%
44
+% SYNTAX
45
+%
46
+%  qe = som_quality(sM,sD);
47
+%  qe = som_quality(sM,D);
48
+%  [qe,te] = som_quality(...);
49
+%
50
+% DESCRIPTION
51
+%
52
+% This function measures the quality of the given map. The measures are
53
+% data-dependent: they measure the map in terms of the given
54
+% data. Typically, the quality of the map is measured in terms of the
55
+% training data. The returned quality measures are average quantization
56
+% error and topographic error.
57
+%
58
+% The issue of SOM quality is a complicated one. Typically two evaluation
59
+% criterias are used: resolution and topology preservation. There are
60
+% many ways to measure them. The ones implemented here were chosen for
61
+% their simplicity.
62
+%
63
+%  qe : Average distance between each data vector and its BMU.
64
+%       Measures map resolution.
65
+%  te : Topographic error, the proportion of all data vectors
66
+%       for which first and second BMUs are not adjacent units.
67
+%       Measures topology preservation.
68
+%
69
+% NOTE: when calculating BMUs of data vectors, the mask of the given 
70
+%       map is used. The mask affects the quantization errors, too.
71
+%       If you want the quantization errors without the weighting given
72
+%       by the mask, you can use the following code: 
73
+%         bmus = som_bmus(sMap,D); % this uses the mask in finding the BMUs
74
+%         for i=1:length(bmus), 
75
+%           dx = sMap.codebook(bmus(i),:)-D(i,:); % m - x
76
+%           dx(isnan(dx)) = 0;                    % remove NaNs 
77
+%           qerr(i) = sqrt(sum(dx.^2));           % euclidian distance
78
+%         end
79
+%         qe = mean(qerr); % average quantization error
80
+%
81
+% Please note that you should _not_ trust the measures blindly. Generally,
82
+% both measures give the best results when the map has overfitted the
83
+% data. This may happen when the number of map units is as large or larger
84
+% than the number of training samples. Beware when you have such a case.
85
+%
86
+% REFERENCES
87
+%
88
+% Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, 
89
+%    Berlin, 1995, pp. 113.
90
+% Kiviluoto, K., "Topology Preservation in Self-Organizing Maps", 
91
+%    in the proceeding of International Conference on Neural
92
+%    Networks (ICNN), 1996, pp. 294-299.
93
+%
94
+% INPUT ARGUMENTS
95
+%
96
+%  sMap    (struct) Map struct.
97
+%  D                The data to be used.
98
+%          (matrix) A data matrix, size dlen x dim.
99
+%          (struct) A data struct.
100
+%
101
+% OUTPUT ARGUMENTS
102
+% 
103
+%  qe      (scalar) mean quantization error
104
+%  te      (scalar) topographic error
105
+%
106
+% EXAMPLES
107
+%
108
+%  qe = som_quality(sMap,D);
109
+%  [qe,te] = som_quality(sMap,sD);
110
+%
111
+% SEE ALSO
112
+% 
113
+%  som_bmus         Find BMUs for the given set of data vectors.
114
+
115
+% Copyright (c) 1997-2000 by the SOM toolbox programming team.
116
+% http://www.cis.hut.fi/projects/somtoolbox/
117
+
118
+% Version 1.0beta juuso 220997
119
+% Version 2.0beta juuso 151199
120
+
121
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122
+%% check arguments
123
+
124
+% input arguments
125
+if nargin < 2, error('Not enough input arguments.'); end
126
+
127
+% data
128
+if isstruct(D), D = D.data; end
129
+[dlen dim] = size(D);
130
+
131
+% calculate topographic error, too?
132
+if nargout==1, b=1; else b=[1:2]; end
133
+[bmus qerrs]= som_bmus(sMap,D,b);
134
+inds = find(~isnan(bmus(:,1)));
135
+bmus = bmus(inds,:);
136
+qerrs = qerrs(inds,:);
137
+l = length(inds);
138
+if ~l, error('Empty data set.'); end
139
+
140
+% mean quantization error
141
+mqe = mean(qerrs(:,1));
142
+
143
+if length(b)==2, % topographic error
144
+  Ne = full(som_unit_neighs(sMap.topol));
145
+  tge = 0;
146
+  for i=1:l, tge = tge+(Ne(bmus(i,1),bmus(i,2)) ~= 1); end
147
+  tge = tge / l;
148
+else
149
+  tge = NaN;
150
+end
151
+
152
+return;
153
+
154
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
+
156
+