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