Daniel@0: function P = sammon(D, P, varargin) Daniel@0: Daniel@0: %SAMMON Computes Sammon's mapping of a data set. Daniel@0: % Daniel@0: % P = sammon(D, P, [value], [mode], [alpha], [Mdist]) Daniel@0: % Daniel@0: % P = sammon(D,2); % projection to 2-dim space Daniel@0: % P = sammon(sMap,3); % projects the codebook vectors Daniel@0: % P = sammon(sMap,3,[],[],[],Md) % uses distance matrix Md Daniel@0: % som_grid(sMap,'Coord',P) % visualization of map projection Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % D (matrix) size dlen x dim, data to be projected Daniel@0: % (struct) data or map struct Daniel@0: % P (scalar) output dimension Daniel@0: % (matrix) size dlen x odim, initial projection matrix Daniel@0: % [value] (scalar) all different modes (the next argument) require Daniel@0: % a value, default = 100 Daniel@0: % [mode] (string) 'steps' or 'errlimit' or 'errchange' or 'seconds', Daniel@0: % see below, default is 'steps' Daniel@0: % [alpha] (scalar) iteration step size, default = 0.2 Daniel@0: % [Dist] (matrix) pairwise distance matrix, size dlen x dlen. Daniel@0: % If the distances in the input space should Daniel@0: % be calculated otherwise than as euclidian Daniel@0: % distances, the distance from each vector Daniel@0: % to each other vector can be given here, Daniel@0: % size dlen x dlen. For example PDIST Daniel@0: % function can be used to calculate the Daniel@0: % distances: Dist = squareform(pdist(D,'mahal')); Daniel@0: % Daniel@0: % P (matrix) size dlen x odim, the projections Daniel@0: % Daniel@0: % The output dimension must be 2 or higher but (naturally) lower Daniel@0: % than data set dimension. Daniel@0: % Daniel@0: % The mode argument determines the end condition for iteration. If Daniel@0: % the mode argument is used, also the value argument has to be Daniel@0: % specified. Different mode possibilities are: Daniel@0: % 'steps' the iteration is terminated when it is run Daniel@0: % 'errlimit' steps, the iteration is terminated when projection error Daniel@0: % is lower than , Daniel@0: % 'errchange' the iteration is terminated when change between Daniel@0: % projection error on two successive iteration rounds Daniel@0: % is less than percent of total error, and Daniel@0: % 'seconds' the iteration is terminated after seconds Daniel@0: % of iteration. Daniel@0: % Daniel@0: % See also CCA, PCAPROJ, SOM_GRID. Daniel@0: Daniel@0: % Reference: Sammon, J.W. Jr., "A nonlinear mapping for data Daniel@0: % structure analysis", IEEE Transactions on Computers, vol. C-18, Daniel@0: % no. 5, 1969, pp. 401-409. Daniel@0: Daniel@0: % Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Vesanto Daniel@0: % Copyright (c) by Juha Vesanto Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % juuso 040100 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: error(nargchk(2, 6, nargin)); % check no. of input arguments is correct Daniel@0: Daniel@0: % input data Daniel@0: if isstruct(D), Daniel@0: if isfield(D, 'data'), D = D.data; % data struct Daniel@0: elseif isfield(D, 'codebook'), D = D.codebook; % map struct Daniel@0: else error('Invalid structure'); Daniel@0: end Daniel@0: end Daniel@0: if any(isnan(D(:))), Daniel@0: error('Cannot make Sammon''s projection for data with unknown components') Daniel@0: end Daniel@0: Daniel@0: % compute data dimensions Daniel@0: orig_si = size(D); Daniel@0: dim = orig_si(end); Daniel@0: noc = prod(orig_si)/dim; Daniel@0: if length(orig_si)>2, D = reshape(D,[noc dim]); end Daniel@0: Daniel@0: % output dimension / initial projection matrix Daniel@0: if prod(size(P))==1, Daniel@0: odim = P; Daniel@0: P = rand(noc,odim)-0.5; Daniel@0: else Daniel@0: si = size(P); Daniel@0: odim = si(end); Daniel@0: if prod(si) ~= noc*odim, Daniel@0: error('Initial projection matrix size does not match data size'); Daniel@0: end Daniel@0: if length(si)>2, P = reshape(P,[noc odim]); end Daniel@0: inds = find(isnan(P)); Daniel@0: if length(inds), P(inds) = rand(size(inds)); end Daniel@0: end Daniel@0: if odim > dim | odim < 2, Daniel@0: error('Output dimension must be within [2, dimension of data]'); Daniel@0: end Daniel@0: Daniel@0: % determine operating mode Daniel@0: if nargin < 3 | isempty(varargin{1}) | isnan(varargin{1}), value=100; Daniel@0: else value = varargin{1}; Daniel@0: end Daniel@0: Daniel@0: if nargin < 4 | isempty(varargin{2}) | isnan(varargin{2}), mode='steps'; Daniel@0: else mode = varargin{2}; Daniel@0: end Daniel@0: switch mode, Daniel@0: case 'steps', runlen = value; Daniel@0: case 'errlimit', errlimit = value; Daniel@0: case 'errchange', errchange = value; e_prev = 0; Daniel@0: case 'seconds', endtime = value; Daniel@0: otherwise, error(['Illegal mode: ' mode]); Daniel@0: end Daniel@0: Daniel@0: % iteration step size Daniel@0: if nargin > 4, alpha = varargin{3}; else alpha = NaN; end Daniel@0: if isempty(alpha) | isnan(alpha), alpha = 0.2; end Daniel@0: Daniel@0: % mutual distances Daniel@0: if nargin > 5, Mdist = varargin{4}; else Mdist = []; end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% initialization Daniel@0: Daniel@0: % these are used quite frequently Daniel@0: noc_x_1 = ones(noc, 1); Daniel@0: odim_x_1 = ones(odim,1); Daniel@0: Daniel@0: % compute mutual distances between vectors Daniel@0: if isempty(Mdist) | all(isnan(Mdist(:))), Daniel@0: fprintf(2, 'computing mutual distances\r'); Daniel@0: dim_x_1 = ones(dim,1); Daniel@0: for i = 1:noc, Daniel@0: x = D(i,:); Daniel@0: Diff = D - x(noc_x_1,:); Daniel@0: N = isnan(Diff); Daniel@0: Diff(find(N)) = 0; Daniel@0: Mdist(:,i) = sqrt((Diff.^2)*dim_x_1); Daniel@0: N = find(sum(N')==dim); %mutual distance unknown Daniel@0: if ~isempty(N), Mdist(N,i) = NaN; end Daniel@0: end Daniel@0: else Daniel@0: % if the distance matrix is output from PDIST function Daniel@0: if size(Mdist,1)==1, Mdist = squareform(Mdist); end Daniel@0: if size(Mdist,1)~=noc, Daniel@0: error('Mutual distance matrix size and data set size do not match'); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% action Daniel@0: Daniel@0: if strcmp(mode, 'seconds'), tic; end; Daniel@0: fprintf(2, 'iterating \r'); Daniel@0: Daniel@0: % sammon iteration Daniel@0: Daniel@0: x = P ; Daniel@0: xu = zeros(noc, odim); Daniel@0: xd = zeros(noc, odim); Daniel@0: dq = zeros(noc, 1); Daniel@0: dr = zeros(noc, 1); Daniel@0: Daniel@0: i = 0; Daniel@0: ready = 0; Daniel@0: while ~ready Daniel@0: for j = 1:noc, Daniel@0: xd = -x + x(j*noc_x_1,:); Daniel@0: xd2 = xd.^2; Daniel@0: dpj = sqrt(sum(xd2'))'; Daniel@0: dq = Mdist(:,j) - dpj; Daniel@0: dr = Mdist(:,j) .* dpj; Daniel@0: ind = find(dr ~= 0); Daniel@0: term = dq(ind) ./ dr(ind); Daniel@0: e1 = sum(xd(ind,:) .* term(:,odim_x_1)); Daniel@0: term2 = ((1.0 + dq(ind) ./ dpj(ind)) ./ dpj(ind)) ./ dr(ind); Daniel@0: e2 = sum(term) - sum(xd2(ind,:) .* term2(:,odim_x_1)); Daniel@0: xu(j,:) = x(j,:) + alpha * e1 ./ abs(e2); Daniel@0: end Daniel@0: Daniel@0: % move the center of mass to the center Daniel@0: Daniel@0: c = sum(xu) / noc; Daniel@0: x = xu - c(noc_x_1, :); Daniel@0: Daniel@0: i = i + 1; Daniel@0: Daniel@0: % compute mapping error Daniel@0: % doing this adds about 25% to computing time Daniel@0: if 0, Daniel@0: e = 0; tot = 0; Daniel@0: for j = 2:noc, Daniel@0: d = Mdist(1:(j - 1), j); Daniel@0: tot = tot + sum(d); Daniel@0: ind = find(d ~= 0); Daniel@0: xd = -x(1:(j - 1), :) + x(j * ones(j - 1, 1), :); Daniel@0: ee = d - sqrt(sum(xd'.^2))'; Daniel@0: e = e + sum(ee(ind).^2 ./ d(ind)); Daniel@0: end Daniel@0: e = e/tot; Daniel@0: fprintf(2, '\r%d iterations, error %f', i, e); Daniel@0: else Daniel@0: fprintf(2, '\r%d iterations', i); Daniel@0: end Daniel@0: Daniel@0: % determine is the iteration ready Daniel@0: Daniel@0: switch mode Daniel@0: case 'steps', Daniel@0: if i == runlen, ready = 1; end; Daniel@0: case 'errlimit', Daniel@0: if e < errlimit, ready = 1; end; Daniel@0: case 'errchange', Daniel@0: if i > 1 Daniel@0: change = 100 * abs(e - e_prev) / e_prev; Daniel@0: if change < errchange, ready = 1; end; Daniel@0: fprintf(2, ', change of error %f %% ', change); Daniel@0: end Daniel@0: e_prev = e; Daniel@0: case 'seconds' Daniel@0: if toc > endtime, ready = 1; end; Daniel@0: fprintf(2, ', elapsed time %f seconds ', toc); Daniel@0: end Daniel@0: fprintf(2, ' '); Daniel@0: Daniel@0: % If you want to see the Sammon's projection plotted (in 2-D and 3-D case), Daniel@0: % execute the code below; it is not in use by default to speed up Daniel@0: % computation. Daniel@0: if 0, Daniel@0: clf Daniel@0: if odim == 1, plot(x(:,1), noc_x_1, 'o'); Daniel@0: elseif odim == 2, plot(x(:,1), x(:,2), 'o'); Daniel@0: else plot3(x(:,1), x(:,2), x(:,3), 'o') Daniel@0: end Daniel@0: drawnow Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: fprintf(2, '\n'); Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% clean up Daniel@0: Daniel@0: % reshape Daniel@0: orig_si(end) = odim; Daniel@0: P = reshape(x, orig_si); Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%