Daniel@0: function sMap = som_lininit(D, varargin) Daniel@0: Daniel@0: %SOM_LININIT Initialize a Self-Organizing Map linearly. Daniel@0: % Daniel@0: % sMap = som_lininit(D, [[argID,] value, ...]) Daniel@0: % Daniel@0: % sMap = som_lininit(D); Daniel@0: % sMap = som_lininit(D,sMap); Daniel@0: % sMap = som_lininit(D,'munits',100,'hexa'); Daniel@0: % Daniel@0: % Input and output arguments ([]'s are optional): Daniel@0: % D The training data. Daniel@0: % (struct) data struct Daniel@0: % (matrix) data matrix, size dlen x dim Daniel@0: % [argID, (string) Parameters affecting the map topology are given Daniel@0: % value] (varies) as argument ID - argument value pairs, listed below. Daniel@0: % sMap (struct) map struct Daniel@0: % Daniel@0: % Here are the valid argument IDs and corresponding values. The values Daniel@0: % which are unambiguous (marked with '*') can be given without the Daniel@0: % preceeding argID. Daniel@0: % 'munits' (scalar) number of map units Daniel@0: % 'msize' (vector) map size Daniel@0: % 'lattice' *(string) map lattice: 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid' Daniel@0: % 'topol' *(struct) topology struct Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % 'map' *(struct) map struct Daniel@0: % 'som_map','sMap' = 'map' Daniel@0: % Daniel@0: % For more help, try 'type som_lininit' or check out online documentation. Daniel@0: % See also SOM_MAP_STRUCT, SOM_RANDINIT, SOM_MAKE. Daniel@0: Daniel@0: %%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: % Daniel@0: % som_lininit Daniel@0: % Daniel@0: % PURPOSE Daniel@0: % Daniel@0: % Initializes a SOM linearly along its greatest eigenvectors. Daniel@0: % Daniel@0: % SYNTAX Daniel@0: % Daniel@0: % sMap = som_lininit(D); Daniel@0: % sMap = som_lininit(D,sMap); Daniel@0: % sMap = som_lininit(D,'munits',100,'hexa'); Daniel@0: % Daniel@0: % DESCRIPTION Daniel@0: % Daniel@0: % Initializes a SOM linearly. If necessary, a map struct is created Daniel@0: % first. The initialization is made by first calculating the eigenvalues Daniel@0: % and eigenvectors of the training data. Then, the map is initialized Daniel@0: % along the mdim greatest eigenvectors of the training data, where Daniel@0: % mdim is the dimension of the map grid. Daniel@0: % Daniel@0: % REFERENCES Daniel@0: % Daniel@0: % Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, Daniel@0: % Berlin, 1995, pp. 106-107. Daniel@0: % Daniel@0: % REQUIRED INPUT ARGUMENTS Daniel@0: % Daniel@0: % D The training data. Daniel@0: % (struct) Data struct. If this is given, its '.comp_names' and Daniel@0: % '.comp_norm' fields are copied to the map struct. Daniel@0: % (matrix) data matrix, size dlen x dim Daniel@0: % Daniel@0: % OPTIONAL INPUT ARGUMENTS Daniel@0: % Daniel@0: % argID (string) Argument identifier string (see below). Daniel@0: % value (varies) Value for the argument (see below). Daniel@0: % Daniel@0: % The optional arguments can be given as 'argID',value -pairs. If an Daniel@0: % argument is given value multiple times, the last one is used. Daniel@0: % Daniel@0: % Here are the valid argument IDs and corresponding values. The values Daniel@0: % which are unambiguous (marked with '*') can be given without the Daniel@0: % preceeding argID. Daniel@0: % 'dlen' (scalar) length of the training data Daniel@0: % 'data' (matrix) the training data Daniel@0: % *(struct) the training data Daniel@0: % 'munits' (scalar) number of map units Daniel@0: % 'msize' (vector) map size Daniel@0: % 'lattice' *(string) map lattice: 'hexa' or 'rect' Daniel@0: % 'shape' *(string) map shape: 'sheet', 'cyl' or 'toroid' Daniel@0: % 'topol' *(struct) topology struct Daniel@0: % 'som_topol','sTopol' = 'topol' Daniel@0: % 'map' *(struct) map struct Daniel@0: % 'som_map','sMap' = 'map' Daniel@0: % Daniel@0: % OUTPUT ARGUMENTS Daniel@0: % Daniel@0: % sMap (struct) The initialized map struct. Daniel@0: % Daniel@0: % EXAMPLES Daniel@0: % Daniel@0: % sMap = som_lininit(D); Daniel@0: % sMap = som_lininit(D,sMap); Daniel@0: % sMap = som_lininit(D,'msize',[10 10]); Daniel@0: % sMap = som_lininit(D,'munits',100,'rect'); Daniel@0: % Daniel@0: % SEE ALSO Daniel@0: % Daniel@0: % som_map_struct Create a map struct. Daniel@0: % som_randinit Initialize a map with random values. Daniel@0: % som_make Initialize and train self-organizing map. 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 ecco 100997 Daniel@0: % Version 2.0beta 101199 Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% check arguments Daniel@0: Daniel@0: % data Daniel@0: if isstruct(D), Daniel@0: data_name = D.name; Daniel@0: comp_names = D.comp_names; Daniel@0: comp_norm = D.comp_norm; Daniel@0: D = D.data; Daniel@0: struct_mode = 1; Daniel@0: else Daniel@0: data_name = inputname(1); Daniel@0: struct_mode = 0; Daniel@0: end Daniel@0: [dlen dim] = size(D); Daniel@0: Daniel@0: % varargin Daniel@0: sMap = []; Daniel@0: sTopol = som_topol_struct; Daniel@0: sTopol.msize = 0; Daniel@0: munits = NaN; Daniel@0: i=1; Daniel@0: while i<=length(varargin), Daniel@0: argok = 1; Daniel@0: if ischar(varargin{i}), Daniel@0: switch varargin{i}, Daniel@0: case 'munits', i=i+1; munits = varargin{i}; sTopol.msize = 0; Daniel@0: case 'msize', i=i+1; sTopol.msize = varargin{i}; Daniel@0: munits = prod(sTopol.msize); Daniel@0: case 'lattice', i=i+1; sTopol.lattice = varargin{i}; Daniel@0: case 'shape', i=i+1; sTopol.shape = varargin{i}; Daniel@0: case {'som_topol','sTopol','topol'}, i=i+1; sTopol = varargin{i}; Daniel@0: case {'som_map','sMap','map'}, i=i+1; sMap = varargin{i}; sTopol = sMap.topol; Daniel@0: case {'hexa','rect'}, sTopol.lattice = varargin{i}; Daniel@0: case {'sheet','cyl','toroid'}, sTopol.shape = varargin{i}; Daniel@0: otherwise argok=0; Daniel@0: end Daniel@0: elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), Daniel@0: switch varargin{i}.type, Daniel@0: case 'som_topol', Daniel@0: sTopol = varargin{i}; Daniel@0: case 'som_map', Daniel@0: sMap = varargin{i}; Daniel@0: sTopol = sMap.topol; Daniel@0: otherwise argok=0; Daniel@0: end Daniel@0: else Daniel@0: argok = 0; Daniel@0: end Daniel@0: if ~argok, Daniel@0: disp(['(som_topol_struct) Ignoring invalid argument #' num2str(i)]); Daniel@0: end Daniel@0: i = i+1; Daniel@0: end Daniel@0: Daniel@0: if length(sTopol.msize)==1, sTopol.msize = [sTopol.msize 1]; end Daniel@0: Daniel@0: if ~isempty(sMap), Daniel@0: [munits dim2] = size(sMap.codebook); Daniel@0: if dim2 ~= dim, error('Map and data must have the same dimension.'); end Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% create map Daniel@0: Daniel@0: % map struct Daniel@0: if ~isempty(sMap), Daniel@0: sMap = som_set(sMap,'topol',sTopol); Daniel@0: else Daniel@0: if ~prod(sTopol.msize), Daniel@0: if isnan(munits), Daniel@0: sTopol = som_topol_struct('data',D,sTopol); Daniel@0: else Daniel@0: sTopol = som_topol_struct('data',D,'munits',munits,sTopol); Daniel@0: end Daniel@0: end Daniel@0: sMap = som_map_struct(dim, sTopol); Daniel@0: end Daniel@0: Daniel@0: if struct_mode, Daniel@0: sMap = som_set(sMap,'comp_names',comp_names,'comp_norm',comp_norm); Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Daniel@0: %% initialization Daniel@0: Daniel@0: % train struct Daniel@0: sTrain = som_train_struct('algorithm','lininit'); Daniel@0: sTrain = som_set(sTrain,'data_name',data_name); Daniel@0: Daniel@0: msize = sMap.topol.msize; Daniel@0: mdim = length(msize); Daniel@0: munits = prod(msize); Daniel@0: Daniel@0: [dlen dim] = size(D); Daniel@0: if dlen<2, Daniel@0: %if dlen==1, sMap.codebook = (sMap.codebook - 0.5)*diag(D); end Daniel@0: error(['Linear map initialization requires at least two NaN-free' ... Daniel@0: ' samples.']); Daniel@0: return; Daniel@0: end Daniel@0: Daniel@0: % compute principle components Daniel@0: if dim > 1 & sum(msize > 1) > 1, Daniel@0: % calculate mdim largest eigenvalues and their corresponding Daniel@0: % eigenvectors Daniel@0: Daniel@0: % autocorrelation matrix Daniel@0: A = zeros(dim); Daniel@0: me = zeros(1,dim); Daniel@0: for i=1:dim, Daniel@0: me(i) = mean(D(isfinite(D(:,i)),i)); Daniel@0: D(:,i) = D(:,i) - me(i); Daniel@0: end Daniel@0: for i=1:dim, Daniel@0: for j=i:dim, Daniel@0: c = D(:,i).*D(:,j); c = c(isfinite(c)); Daniel@0: A(i,j) = sum(c)/length(c); A(j,i) = A(i,j); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % take mdim first eigenvectors with the greatest eigenvalues Daniel@0: [V,S] = eig(A); Daniel@0: eigval = diag(S); Daniel@0: [y,ind] = sort(eigval); Daniel@0: eigval = eigval(flipud(ind)); Daniel@0: V = V(:,flipud(ind)); Daniel@0: V = V(:,1:mdim); Daniel@0: eigval = eigval(1:mdim); Daniel@0: Daniel@0: % normalize eigenvectors to unit length and multiply them by Daniel@0: % corresponding (square-root-of-)eigenvalues Daniel@0: for i=1:mdim, V(:,i) = (V(:,i) / norm(V(:,i))) * sqrt(eigval(i)); end Daniel@0: Daniel@0: else Daniel@0: Daniel@0: me = zeros(1,dim); Daniel@0: V = zeros(1,dim); Daniel@0: for i=1:dim, Daniel@0: inds = find(~isnan(D(:,i))); Daniel@0: me(i) = mean(D(inds,i),1); Daniel@0: V(i) = std(D(inds,i),1); Daniel@0: end Daniel@0: Daniel@0: end Daniel@0: Daniel@0: % initialize codebook vectors Daniel@0: if dim>1, Daniel@0: sMap.codebook = me(ones(munits,1),:); Daniel@0: Coords = som_unit_coords(msize,'rect','sheet'); Daniel@0: cox = Coords(:,1); Coords(:,1) = Coords(:,2); Coords(:,2) = cox; Daniel@0: for i=1:mdim, Daniel@0: ma = max(Coords(:,i)); mi = min(Coords(:,i)); Daniel@0: if ma>mi, Coords(:,i) = (Coords(:,i)-mi)/(ma-mi); else Coords(:,i) = 0.5; end Daniel@0: end Daniel@0: Coords = (Coords-0.5)*2; Daniel@0: for n = 1:munits, Daniel@0: for d = 1:mdim, Daniel@0: sMap.codebook(n,:) = sMap.codebook(n,:)+Coords(n,d)*V(:, d)'; Daniel@0: end Daniel@0: end Daniel@0: else Daniel@0: sMap.codebook = [0:(munits-1)]'/(munits-1)*(max(D)-min(D))+min(D); Daniel@0: end Daniel@0: Daniel@0: % training struct Daniel@0: sTrain = som_set(sTrain,'time',datestr(now,0)); Daniel@0: sMap.trainhist = sTrain; Daniel@0: Daniel@0: return; Daniel@0: Daniel@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%