annotate toolboxes/MIRtoolbox1.3.2/somtoolbox/som_quality.m @ 0:cc4b1211e677 tip

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