Announce

PukiWiki contents have been moved into SONOTS Plugin (20070703)

Demo: Supervised classification using Self-organizing Map

Developersonots
First Edition04/2005
Last Modified05/2006
LanguageMatlab
Table of Contents

This page is written in both English and Japanese.

Abstract

This is an experiment or a note rather than a software of SOM. SOM itself is an unsupervised clustering technique, but we are using it for supervised classification.

somTest.png

Source Codes

% doSom: Supervised classification using Self-Organizing Map (SOM is 
%   actually an unsupervised clustering technique. )
%
%  [C] = doSom(data, proto, protoClass)
%
%  Input and output arguments ([]'s are optional):
%   data        (matrix) of size NxD. N is the number of data (classifiee)
%                vectors, and D is the dimension of each vector. 
%   proto       (matrix) of size PxD. P is the number of prototype vectors,
%                and D is the dimension of each vector. 
%   protoClass  (vector) of size Px1 that contains integer class labels of 
%                prototypes. protoClass(j) is the class of jth prototype.
%   [UNIT]      (scalar) # of SOM units. UNIT == # of class will avoid
%                the risk to be classified as unknown anyway. 
%   C           (vector) of size Nx1: integers indicating the class 
%                decision for data items. C(i) is the classification for
%                data item i.
%
% Requirement: somtoolbox
%
% Author : Naotoshi Seo
% Date   : April, 2005
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function C = doSom(data, proto, protoClass, UNIT)
 % addpath('somtoolbox');
 if nargin < 4
     classLabel = unique(protoClass);
     nClass     = length(classLabel);
     UNIT = nClass; % # of SOM units
 end
 if size(data,2) ~= size(proto,2)
     error('Dimension of data vectors and prototype vectors do not match.');
 end
 if size(proto,1) ~= size(protoClass,1)
     error('Row # of prototypes and prototype class vector do not match.');
 end

 %%%% Training
 %sMap = som_randinit(proto);       % init randomly
 %sMap = som_seqtrain(sMap, proto); % sequential train
 sMap = som_make(proto, 'munits', UNIT); % equals to randinit and seqtrain
 % labeling proto class to SOM units (make SOM supervised)
 protoSom = som_data_struct(proto,'labels',num2str(protoClass));
 sMap = som_autolabel(sMap, protoSom); 
 % plot SOM space
 %som_grid(sMap, 'Label', sMap.labels, 'LabelSize', 30); 

 %%%%% Testing 
 [Bmus, Qerrors] = som_bmus(sMap, data); % find best matching units 
 % labeling
 %sMap.labels
 dataClassified = sMap.labels(Bmus);
 for i=1:length(dataClassified)
     if isempty(dataClassified{i}), dataClassified{i} = '0', end;
 end
 C = cellfun(@str2num, dataClassified);

doSomTest.m

Source Codes

function doSomTest
 proto = [
    0.6213    0.7373
    0.5226    0.8939
    0.9797    0.6614
    0.9568    0.0118
    0.8801    0.1991
    0.8757    0.0648 
    0.1730    0.2987
    0.2714    0.2844
    0.2523    0.4692
    ];
 protoClass = [
     1
     1
     1
     2
     2
     2 
     3
     3
     3
     ];
 data = [
    0.9883    0.4329
    0.5828    0.2259
    0.4235    0.5798
    0.5155    0.7604
    0.3340    0.5298 
    ];

 % main
 dataClass = doSom(data, proto, protoClass, 3);

 % plot proto
 figure(2);
 classLabel = unique(protoClass);
 nClass     = length(classLabel);
 plotLabel = {'r*', 'g*', 'b*'};
 for i=1:nClass
     A = proto(protoClass == classLabel(i), :);
     plot(A(:, 1), A(:, 2), plotLabel{i});
     hold on;
 end

 % plot data
 plotLabel = {'ro', 'go', 'bo'};
 for i=1:nClass
     A = data(dataClass == classLabel(i), :);
     plot(A(:, 1), A(:, 2), plotLabel{i});
     hold on;
 end
 legend('1: proto','2: proto', '3: proto', '1: data', '2: data', '3: data');
 title('Self-organizing Map');

Results

somTest.png

References

  • I leaned in a lecture. I will try to find good resource later.
  • somtoolbox