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