Daniel@0: function [mqe,tge] = som_quality(sMap, D) Daniel@0: Daniel@0: %SOM_QUALITY Calculate the mean quantization and topographic error. Daniel@0: % Daniel@0: % [qe,te] = som_quality(sMap, D) Daniel@0: % Daniel@0: % qe = som_quality(sMap,D); Daniel@0: % [qe,te] = som_quality(sMap,sD); Daniel@0: % Daniel@0: % Input and output arguments: Daniel@0: % sMap (struct) a map struct Daniel@0: % D the data Daniel@0: % (struct) a data struct Daniel@0: % (matrix) a data matrix, size dlen x dim Daniel@0: % Daniel@0: % qe (scalar) mean quantization error Daniel@0: % te (scalar) topographic error Daniel@0: % Daniel@0: % The issue of SOM quality is a complicated one. Typically two Daniel@0: % evaluation criterias are used: resolution and topology preservation. Daniel@0: % If the dimension of the data set is higher than the dimension of the Daniel@0: % map grid, these usually become contradictory goals. Daniel@0: % Daniel@0: % The first value returned by this function measures resolution and the Daniel@0: % second the topology preservation. Daniel@0: % qe : Average distance between each data vector and its BMU. Daniel@0: % te : Topographic error, the proportion of all data vectors Daniel@0: % for which first and second BMUs are not adjacent units. Daniel@0: % Daniel@0: % NOTE: when calculating BMUs of data vectors, the mask of the given Daniel@0: % map is used. Daniel@0: % Daniel@0: % For more help, try 'type som_quality' or check out the online documentation. Daniel@0: % See also SOM_BMUS. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_quality Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Calculates two quality measures for the given map. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % qe = som_quality(sM,sD); Daniel@0: % qe = som_quality(sM,D); Daniel@0: % [qe,te] = som_quality(...); Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % This function measures the quality of the given map. The measures are Daniel@0: % data-dependent: they measure the map in terms of the given Daniel@0: % data. Typically, the quality of the map is measured in terms of the Daniel@0: % training data. The returned quality measures are average quantization Daniel@0: % error and topographic error. Daniel@0: % Daniel@0: % The issue of SOM quality is a complicated one. Typically two evaluation Daniel@0: % criterias are used: resolution and topology preservation. There are Daniel@0: % many ways to measure them. The ones implemented here were chosen for Daniel@0: % their simplicity. Daniel@0: % Daniel@0: % qe : Average distance between each data vector and its BMU. Daniel@0: % Measures map resolution. Daniel@0: % te : Topographic error, the proportion of all data vectors Daniel@0: % for which first and second BMUs are not adjacent units. Daniel@0: % Measures topology preservation. Daniel@0: % Daniel@0: % NOTE: when calculating BMUs of data vectors, the mask of the given Daniel@0: % map is used. The mask affects the quantization errors, too. Daniel@0: % If you want the quantization errors without the weighting given Daniel@0: % by the mask, you can use the following code: Daniel@0: % bmus = som_bmus(sMap,D); % this uses the mask in finding the BMUs Daniel@0: % for i=1:length(bmus), Daniel@0: % dx = sMap.codebook(bmus(i),:)-D(i,:); % m - x Daniel@0: % dx(isnan(dx)) = 0; % remove NaNs Daniel@0: % qerr(i) = sqrt(sum(dx.^2)); % euclidian distance Daniel@0: % end Daniel@0: % qe = mean(qerr); % average quantization error Daniel@0: % Daniel@0: % Please note that you should _not_ trust the measures blindly. Generally, Daniel@0: % both measures give the best results when the map has overfitted the Daniel@0: % data. This may happen when the number of map units is as large or larger Daniel@0: % than the number of training samples. Beware when you have such a case. Daniel@0: % Daniel@0: % REFERENCES Daniel@0: % Daniel@0: % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, Daniel@0: % Berlin, 1995, pp. 113. Daniel@0: % Kiviluoto, K., "Topology Preservation in Self-Organizing Maps", Daniel@0: % in the proceeding of International Conference on Neural Daniel@0: % Networks (ICNN), 1996, pp. 294-299. Daniel@0: % Daniel@0: % INPUT ARGUMENTS Daniel@0: % Daniel@0: % sMap (struct) Map struct. Daniel@0: % D The data to be used. Daniel@0: % (matrix) A data matrix, size dlen x dim. Daniel@0: % (struct) A data struct. Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % qe (scalar) mean quantization error Daniel@0: % te (scalar) topographic error Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % qe = som_quality(sMap,D); Daniel@0: % [qe,te] = som_quality(sMap,sD); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_bmus Find BMUs for the given set of data vectors. Daniel@0: Daniel@0: % Copyright (c) 1997-2000 by the SOM toolbox programming team. Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 1.0beta juuso 220997 Daniel@0: % Version 2.0beta juuso 151199 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: % input arguments Daniel@0: if nargin < 2, error('Not enough input arguments.'); end Daniel@0: Daniel@0: % data Daniel@0: if isstruct(D), D = D.data; end Daniel@0: [dlen dim] = size(D); Daniel@0: Daniel@0: % calculate topographic error, too? Daniel@0: if nargout==1, b=1; else b=[1:2]; end Daniel@0: [bmus qerrs]= som_bmus(sMap,D,b); Daniel@0: inds = find(~isnan(bmus(:,1))); Daniel@0: bmus = bmus(inds,:); Daniel@0: qerrs = qerrs(inds,:); Daniel@0: l = length(inds); Daniel@0: if ~l, error('Empty data set.'); end Daniel@0: Daniel@0: % mean quantization error Daniel@0: mqe = mean(qerrs(:,1)); Daniel@0: Daniel@0: if length(b)==2, % topographic error Daniel@0: Ne = full(som_unit_neighs(sMap.topol)); Daniel@0: tge = 0; Daniel@0: for i=1:l, tge = tge+(Ne(bmus(i,1),bmus(i,2)) ~= 1); end Daniel@0: tge = tge / l; Daniel@0: else Daniel@0: tge = NaN; Daniel@0: end Daniel@0: Daniel@0: return; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: Daniel@0: