Browse code

starting som prediction fine-tuned class-performance visualisation

git-svn-id: https://svn.discofish.de/MATLAB/spmtoolbox/SVMCrossVal@112 83ab2cfd-5345-466c-8aeb-2b2739fb922d

Christoph Budziszewski authored on21/01/2009 16:34:25
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,337 @@
1
+function sM  = som_supervised(sData,varargin)
2
+
3
+%SOM_SUPERVISED SOM training which utilizes class information.
4
+%
5
+% sM = som_supervised(sData, [ArgID, value,...]))
6
+%
7
+%  Input and output arguments ([]'s are optional)
8
+%   sData    (struct) data struct, the class information is 
9
+%                     taken from the first column of .labels field
10
+%   [argID,  (string) See below. These are given as 
11
+%    value]  (varies) 'argID', value -pairs.
12
+%
13
+%   sMap     (struct) map struct
14
+%
15
+%  Here are the argument IDs and corresponding values: 
16
+%  'munits'     (scalar) the preferred number of map units
17
+%  'msize'      (vector) map grid size
18
+%  'mask'       (vector) BMU search mask, size dim x 1
19
+%  'name'       (string) map name
20
+%  'comp_names' (string array / cellstr) component names, size dim x 1
21
+%  'tracking'   (scalar) how much to report, default = 1
22
+%  The following values are unambiguous and can therefore
23
+%  be given without the preceeding argument ID:
24
+%  'algorithm'  (string) training algorithm: 'seq' or 'batch'
25
+%  'mapsize'    (string) do you want a 'small', 'normal' or 'big' map
26
+%               Any explicit settings of munits or msize override this.
27
+%  'topol'      (struct) topology struct
28
+%  'som_topol','sTopol' = 'topol'
29
+%  'lattice'    (string) map lattice, 'hexa' or 'rect'
30
+%  'shape'      (string) map shape, 'sheet', 'cyl' or 'toroid'
31
+%  'neigh'      (string) neighborhood function, 'gaussian', 'cutgauss',
32
+%                       'ep' or 'bubble'
33
+%
34
+% For more help, try 'type som_supervised', or check out online documentation.
35
+% See also SOM_MAKE, SOM_AUTOLABEL.
36
+
37
+%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38
+%
39
+% som_supervised
40
+%
41
+% PURPOSE
42
+%
43
+% Creates, initializes and trains a supervised SOM by taking the 
44
+% class-identity into account.
45
+%
46
+% SYNTAX
47
+%
48
+% sMap = som_supervised(sData);
49
+% sMap = som_supervised(...,'argID',value,...)
50
+% sMap = som_make(...,value,...);
51
+%
52
+% DESCRIPTION
53
+%
54
+% Creates, initializes and trains a supervised SOM. It constructs the
55
+% training data by adding 1-of-N -coded matrix to the original data
56
+% based on the class information in the .labels field. The dimension
57
+% of vectors after the process is (the old dimension + number of
58
+% different classes). In each vector, one of the new components has
59
+% value '1' (this depends on the class of the vector), and others '0'.
60
+% Calls SOM_MAKE to construct the map. Then the class of each map unit
61
+% is determined by taking maximum over these added components, and a
62
+% label is give accordingly. Finally, the extra components (the
63
+% 1-of-N -coded ones) are removed.
64
+%
65
+% REFERENCES
66
+%
67
+% Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, 
68
+%    Berlin, 1995, pp. 160-161.
69
+% Kohonen, T., M�kivasara, K., Saram�ki, T., "Phonetic Maps - 
70
+%    Insightful Representation of Phonological Features For 
71
+%    Speech Recognition", In proceedings of International
72
+%    Conference on Pattern Recognition (ICPR), Montreal, Canada, 
73
+%    1984, pp. 182-185.
74
+%
75
+% REQUIRED INPUT ARGUMENTS
76
+%
77
+% sData           The data to use in the training.
78
+%        (struct) A data struct. '.comp_names' as well as '.name' 
79
+%                 is copied to the map. The class information is 
80
+%                 taken from the first column of '.labels' field.
81
+%
82
+% OPTIONAL INPUT ARGUMENTS 
83
+%
84
+%  argID (string) Argument identifier string (see below).
85
+%  value (varies) Value for the argument (see below).
86
+%
87
+%  The optional arguments can be given as 'argID',value -pairs. If an
88
+%  argument is given value multiple times, the last one is used. 
89
+%  Here are the argument IDs and corresponding values: 
90
+%   'munits'     (scalar) the preferred number of map units - this may 
91
+%                 change a bit, depending on the properties of the data
92
+%   'msize'      (vector) map grid size
93
+%   'mask'       (vector) BMU search mask, size dim x 1
94
+%   'name'       (string) map name
95
+%   'comp_names' (string array / cellstr) component names, size dim x 1
96
+%   'tracking'   (scalar) how much to report, default = 1. This parameter 
97
+%                 is also passed to the training functions. 
98
+%   The following values are unambiguous and can therefore
99
+%   be given without the preceeding argument ID:
100
+%   'algorithm'  (string) training algorithm: 'seq' or 'batch' (default)
101
+%   'mapsize'    (string) do you want a 'small', 'normal' or 'big' map
102
+%                 Any explicit settings of munits or msize (or topol) 
103
+%                 override this.
104
+%   'topol'      (struct) topology struct
105
+%   'som_topol','sTopol' = 'topol'
106
+%   'lattice'    (string) map lattice, 'hexa' or 'rect'
107
+%   'shape'      (string) map shape, 'sheet', 'cyl' or 'toroid'
108
+%   'neigh'      (string) neighborhood function, 'gaussian', 'cutgauss',
109
+%                 'ep' or 'bubble'
110
+%
111
+% OUTPUT ARGUMENTS
112
+% 
113
+%  sMap (struct)  SOM -map struct
114
+%
115
+% EXAMPLES
116
+%
117
+%  To simply train a map with default parameters:
118
+%
119
+%   sMap = som_supervised(sData);
120
+%
121
+%  With the optional arguments, the initialization and training can be
122
+%  influenced. To change map size, use 'msize', 'munits' or 'mapsize'
123
+%  arguments:  
124
+%
125
+%   sMap = som_supervised(D,'mapsize','big'); or 
126
+%   sMap = som_supervised(D,'big');
127
+%   sMap = som_supervised(D,'munits', 100);
128
+%   sMap = som_supervised(D,'msize', [20 10]); 
129
+%
130
+%  Argument 'algorithm' can be used to switch between 'seq' and 'batch'
131
+%  algorithms. 'batch' is the default, so to use 'seq' algorithm: 
132
+%
133
+%   sMap = som_supervised(D,'algorithm','seq'); or 
134
+%   sMap = som_supervised(D,'seq'); 
135
+%
136
+%  The 'tracking' argument can be used to control the amout of reporting
137
+%  during training. The argument is used in this function, and it is
138
+%  passed to the training functions. To make the function work silently
139
+%  set it to 0.
140
+%
141
+%   sMap = som_supervised(D,'tracking',0); 
142
+%
143
+% SEE ALSO
144
+% 
145
+%  som_make         Create, initialize and train Self-Organizing map.
146
+%  som_autolabel    Label SOM/data set based on another SOM/data set.
147
+
148
+% Contributed to SOM Toolbox vs2, Feb 2nd, 2000 by Juha Parhankangas
149
+% Copyright (c) by Juha Parhankangas
150
+% http://www.cis.hut.fi/projects/somtoolbox/
151
+
152
+% Juha Parhankangas 050100
153
+
154
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
+
156
+D0 = sData.data;
157
+[c,n,classlabels] = class2num(sData.labels(:,1));
158
+
159
+%%%%%%%% Checking arguments %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160
+
161
+if ~isstruct(sData)
162
+  error('Argument ''sData'' must be a ''som_data'' -struct.');
163
+else
164
+  data_name = sData.name;
165
+  comp_names = sData.comp_names;
166
+  comp_norm = sData.comp_norm;
167
+end
168
+
169
+[dlen,dim] = size(sData.data);
170
+
171
+% defaults
172
+
173
+mapsize = '';
174
+sM = som_map_struct(dim+n); 
175
+sTopol = sM.topol;
176
+munits = prod(sTopol.msize); % should be zero
177
+mask = sM.mask; 
178
+name = sM.name; 
179
+neigh = sM.neigh; 
180
+tracking = 1;
181
+algorithm = 'batch'; 
182
+
183
+%%%% changes to defaults (checking varargin) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184
+
185
+i=1; 
186
+while i <= length(varargin) 
187
+  argok = 1; 
188
+  if ischar(varargin{i}) 
189
+    switch varargin{i}, 
190
+      % argument IDs
191
+     case 'mask',       
192
+      i=i+1; 
193
+      mask = varargin{i}; 
194
+     case 'munits',     
195
+      i=i+1; 
196
+      munits = varargin{i}; 
197
+     case 'msize',      
198
+      i=i+1; 
199
+      sTopol.msize = varargin{i}; 
200
+      munits = prod(sTopol.msize); 
201
+     case 'mapsize',    
202
+      i=i+1; 
203
+      mapsize = varargin{i}; 
204
+     case 'name',       
205
+      i=i+1; 
206
+      name = varargin{i};
207
+     case 'comp_names', 
208
+      i=i+1; 
209
+      comp_names = varargin{i}; 
210
+     case 'lattice',    
211
+      i=i+1; 
212
+      sTopol.lattice = varargin{i};
213
+     case 'shape',      
214
+      i=i+1; 
215
+      sTopol.shape = varargin{i}; 
216
+     case {'topol','som_topol','sTopol'}, 
217
+      i=i+1; 
218
+      sTopol = varargin{i}; 
219
+      munits = prod(sTopol.msize); 
220
+     case 'neigh',      
221
+      i=i+1; 
222
+      neigh = varargin{i};
223
+     case 'tracking',   
224
+      i=i+1; 
225
+      tracking = varargin{i};
226
+     case 'algorithm',  
227
+      i=i+1; 
228
+      algorithm = varargin{i}; 
229
+  % unambiguous values
230
+     case {'hexa','rect'}, 
231
+      sTopol.lattice = varargin{i};
232
+     case {'sheet','cyl','toroid'}, 
233
+      sTopol.shape = varargin{i}; 
234
+     case {'gaussian','cutgauss','ep','bubble'}, 
235
+      neigh = varargin{i};
236
+     case {'seq','batch'}, 
237
+      algorithm = varargin{i}; 
238
+     case {'small','normal','big'}, 
239
+      mapsize = varargin{i}; 
240
+     otherwise argok=0; 
241
+    end
242
+  elseif isstruct(varargin{i}) & isfield(varargin{i},'type'), 
243
+    switch varargin{i}(1).type, 
244
+      case 'som_topol', 
245
+       sTopol = varargin{i}; 
246
+     otherwise argok=0; 
247
+    end
248
+  else
249
+    argok = 0; 
250
+  end
251
+  if ~argok, 
252
+    disp(['(som_supervised) Ignoring invalid argument #' num2str(i+1)]); 
253
+  end
254
+  i = i+1; 
255
+end
256
+
257
+%%%%%%%% Action %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258
+
259
+
260
+
261
+% constructing the training data by adding 1-of-N -coded matrix to the
262
+% original data.
263
+
264
+[dlen,dim] = size(D0);
265
+
266
+Dc = zeros(dlen,n);
267
+
268
+for i=1:dlen 
269
+  if c(i)
270
+    Dc(i,c(i)) = 1;
271
+  end
272
+end
273
+
274
+D = [D0, Dc];
275
+
276
+% initialization and training 
277
+
278
+sD = som_data_struct(D,...
279
+                     'name',data_name);
280
+
281
+sM = som_make(sD,...
282
+              'mask',mask,...
283
+              'munits',munits,...
284
+              'name',data_name,...
285
+              'tracking',tracking,...
286
+              'algorithm',algorithm,...
287
+              'mapsize',mapsize,...
288
+              'topol',sTopol,...
289
+              'neigh',neigh);
290
+
291
+% add labels
292
+
293
+for i=1:prod(sM.topol.msize), 
294
+  [dummy,class] = max(sM.codebook(i,dim+[1:n]));
295
+  sM.labels{i} = classlabels{class};
296
+end
297
+
298
+%sD.labels = sData.labels;
299
+%sM = som_autolabel(sM,sD,'vote');
300
+
301
+% remove extra components and modify map -struct
302
+
303
+sM.codebook = sM.codebook(:,1:dim);
304
+sM.mask = sM.mask(1:dim);
305
+sM.comp_names = sData.comp_names;
306
+sM.comp_norm = sData.comp_norm;
307
+
308
+% remove extras from sM.trainhist
309
+
310
+for i=1:length(sM.trainhist)
311
+  if sM.trainhist(i).mask
312
+    sM.trainhist(i).mask = sM.trainhist(i).mask(1:dim);
313
+  end
314
+end
315
+
316
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317
+
318
+function [numbers, n, names] = class2num(class)
319
+
320
+names = {};
321
+numbers = zeros(length(class),1);
322
+
323
+for i=1:length(class)
324
+  if ~isempty(class{i}) & ~any(strcmp(class{i},names))
325
+    names=cat(1,names,class(i));
326
+  end
327
+end
328
+
329
+n=length(names);
330
+
331
+tmp_numbers = (1:n)';
332
+
333
+for i=1:length(class)
334
+  if ~isempty(class{i})
335
+    numbers(i,1) = find(strcmp(class{i},names));    
336
+  end
337
+end