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,180 @@
1
+function codebook=lvq1(codebook, data, rlen, alpha);
2
+
3
+%LVQ1 Trains a codebook with the LVQ1 -algorithm.
4
+%
5
+%  sM = lvq1(sM, D, rlen, alpha)
6
+%
7
+%   sM = lvq1(sM,sD,30*length(sM.codebook),0.08);
8
+%
9
+%  Input and output arguments: 
10
+%   sM    (struct) map struct, the class information must be 
11
+%                  present on the first column of .labels field
12
+%   D     (struct) data struct, the class information must
13
+%                  be present on the first column of .labels field
14
+%   rlen  (scalar) running length
15
+%   alpha (scalar) learning parameter
16
+%
17
+%   sM    (struct) map struct, the trained codebook
18
+%
19
+% NOTE: does not take mask into account.
20
+% 
21
+% For more help, try 'type lvq1', or check out online documentation. 
22
+% See also LVQ3, SOM_SUPERVISED, SOM_SEQTRAIN.
23
+
24
+%%%%%%%%%%%%% DETAILED DESCRIPTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
25
+%
26
+% lvq1
27
+%
28
+% PURPOSE
29
+%
30
+% Trains codebook with the LVQ1 -algorithm (described below).
31
+%
32
+% SYNTAX
33
+%
34
+%  sM = lvq1(sM, D, rlen, alpha)
35
+%
36
+% DESCRIPTION
37
+%
38
+% Trains codebook with the LVQ1 -algorithm. Codebook contains a number
39
+% of vectors (mi, i=1,2,...,n) and so does data (vectors xj,
40
+% j=1,2,...,k).  Both vector sets are classified: vectors may have a
41
+% class (classes are set to the first column of data or map -structs'
42
+% .labels -field). For each xj there is defined the nearest codebook
43
+% -vector index c by searching the minimum of the euclidean distances
44
+% between the current xj and codebook -vectors:
45
+%
46
+%    c = min{ ||xj - mi|| },  i=[1,..,n], for fixed xj
47
+%         i
48
+% If xj and mc belong to the same class, mc is updated as follows:
49
+%    mc(t+1) = mc(t) + alpha * (xj(t) - mc(t))
50
+% If xj and mc belong to different classes, mc is updated as follows:
51
+%    mc(t+1) = mc(t) - alpha * (xj(t) - mc(t))
52
+% Otherwise updating is not performed.
53
+% 
54
+% Argument 'rlen' tells how many times training sequence is performed.
55
+% LVQ1 -algorithm may be stopped after a number of steps, that is
56
+% 30-50 times the number of codebook vectors.
57
+%
58
+% Argument 'alpha' is the learning rate, recommended to be smaller
59
+% than 0.1.
60
+%
61
+% NOTE: does not take mask into account.
62
+%
63
+% REFERENCES
64
+%
65
+% Kohonen, T., "Self-Organizing Map", 2nd ed., Springer-Verlag, 
66
+%    Berlin, 1995, pp. 176-179.
67
+%
68
+% See also LVQ_PAK from http://www.cis.hut.fi/research/som_lvq_pak.shtml
69
+%   
70
+% REQUIRED INPUT ARGUMENTS
71
+%
72
+%  sM                The data to be trained.
73
+%          (struct)  A map struct.
74
+%
75
+%  D                 The data to use in training.
76
+%          (struct)  A data struct.
77
+%
78
+%  rlen    (integer) Running length of LVQ1 -algorithm.
79
+%                    
80
+%  alpha   (float)   Learning rate used in training.
81
+%
82
+% OUTPUT ARGUMENTS
83
+%
84
+%  codebook          Trained data.
85
+%          (struct)  A map struct.
86
+%
87
+% EXAMPLE
88
+%
89
+%   lab = unique(sD.labels(:,1));         % different classes
90
+%   mu = length(lab)*5;                   % 5 prototypes for each    
91
+%   sM = som_randinit(sD,'msize',[mu 1]); % initial prototypes
92
+%   sM.labels = [lab;lab;lab;lab;lab];    % their classes
93
+%   sM = lvq1(sM,sD,50*mu,0.05);          % use LVQ1 to adjust
94
+%                                         % the prototypes      
95
+%   sM = lvq3(sM,sD,50*mu,0.05,0.2,0.3);  % then use LVQ3 
96
+%
97
+% SEE ALSO
98
+% 
99
+%  lvq3             Use LVQ3 algorithm for training.
100
+%  som_supervised   Train SOM using supervised training.
101
+%  som_seqtrain     Train SOM with sequential algorithm.
102
+
103
+% Contributed to SOM Toolbox vs2, February 2nd, 2000 by Juha Parhankangas
104
+% Copyright (c) Juha Parhankangas
105
+% http://www.cis.hut.fi/projects/somtoolbox/
106
+
107
+% Juha Parhankangas 310100 juuso 020200
108
+
109
+cod = codebook.codebook;
110
+c_class = class2num(codebook.labels(:,1));
111
+
112
+dat = data.data;
113
+d_class = class2num(data.labels(:,1));
114
+
115
+x=size(dat,1);
116
+y=size(cod,2);
117
+
118
+ONES=ones(size(cod,1),1);
119
+
120
+for t=1:rlen
121
+
122
+  fprintf(1,'\rTraining round: %d',t);
123
+  tmp=NaN*ones(x,y);
124
+
125
+  for j=1:x
126
+    no_NaN=find(~isnan(dat(j,:)));
127
+    di = sqrt(sum([cod(:,no_NaN)  - ONES*dat(j,no_NaN)].^2,2));
128
+
129
+    [foo,ind] = min(di);
130
+
131
+    if d_class(j) & d_class(j) == c_class(ind) % 0 is for unclassified vectors
132
+      tmp(ind,:) = cod(ind,:) + alpha * (dat(j,:) - cod(ind,:));
133
+    elseif d_class(j)
134
+      tmp(ind,:) = cod(ind,:) - alpha*(dat(j,:) - cod(ind,:));
135
+    end
136
+  end
137
+
138
+  inds = find(~isnan(sum(tmp,2)));
139
+  cod(inds,:) = tmp(inds,:);
140
+end
141
+
142
+codebook.codebook = cod;
143
+
144
+sTrain = som_set('som_train','algorithm','lvq1',...
145
+		 'data_name',data.name,...
146
+		 'neigh','',...
147
+		 'mask',ones(y,1),...
148
+		 'radius_ini',NaN,...
149
+		 'radius_fin',NaN,...
150
+		 'alpha_ini',alpha,...
151
+		 'alpha_type','constant',...
152
+		 'trainlen',rlen,...
153
+		 'time',datestr(now,0));
154
+codebook.trainhist(end+1) = sTrain;
155
+
156
+return;
157
+
158
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159
+
160
+function nos = class2num(class)
161
+
162
+names = {};
163
+nos = zeros(length(class),1);
164
+
165
+for i=1:length(class)
166
+  if ~isempty(class{i}) & ~any(strcmp(class{i},names))
167
+    names=cat(1,names,class(i));
168
+  end
169
+end
170
+
171
+tmp_nos = (1:length(names))';
172
+
173
+for i=1:length(class)
174
+  if ~isempty(class{i})
175
+    nos(i,1) = find(strcmp(class{i},names));    
176
+  end
177
+end
178
+
179
+
180
+