Daniel@0: Daniel@0: %SOM_DEMO4 Data analysis using the SOM. Daniel@0: Daniel@0: % Contributed to SOM Toolbox 2.0, February 11th, 2000 by Juha Vesanto Daniel@0: % http://www.cis.hut.fi/projects/somtoolbox/ Daniel@0: Daniel@0: % Version 1.0beta juuso 071197 Daniel@0: % Version 2.0beta juuso 090200 070600 Daniel@0: Daniel@0: clf reset; Daniel@0: f0 = gcf; Daniel@0: echo on Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: clc Daniel@0: % ========================================================== Daniel@0: % SOM_DEMO4 - DATA ANALYSIS USING THE SOM Daniel@0: % ========================================================== Daniel@0: Daniel@0: % In this demo, the IRIS data set is analysed using SOM. First, the Daniel@0: % data is read from ascii file (please make sure they can be found Daniel@0: % from the current path), normalized, and a map is Daniel@0: % trained. Since the data also had labels, the map is labelled. Daniel@0: Daniel@0: try, Daniel@0: sD = som_read_data('iris.data'); Daniel@0: catch Daniel@0: echo off Daniel@0: Daniel@0: warning('File ''iris.data'' not found. Using simulated data instead.') Daniel@0: Daniel@0: D = randn(50,4); Daniel@0: D(:,1) = D(:,1)+5; D(:,2) = D(:,2)+3.5; Daniel@0: D(:,3) = D(:,3)/2+1.5; D(:,4) = D(:,4)/2+0.3; Daniel@0: D2 = randn(100,4); D2(:,2) = sort(D2(:,2)); Daniel@0: D2(:,1) = D2(:,1)+6.5; D2(:,2) = D2(:,2)+2.8; Daniel@0: D2(:,3) = D2(:,3)+5; D2(:,4) = D2(:,4)/2+1.5; Daniel@0: sD = som_data_struct([D; D2],'name','iris (simulated)',... Daniel@0: 'comp_names',{'SepalL','SepalW','PetalL','PetalW'}); Daniel@0: sD = som_label(sD,'add',[1:50]','Setosa'); Daniel@0: sD = som_label(sD,'add',[51:100]','Versicolor'); Daniel@0: sD = som_label(sD,'add',[101:150]','Virginica'); Daniel@0: Daniel@0: echo on Daniel@0: end Daniel@0: Daniel@0: sD = som_normalize(sD,'var'); Daniel@0: sM = som_make(sD); Daniel@0: sM = som_autolabel(sM,sD,'vote'); Daniel@0: Daniel@0: pause % Strike any key to visualize the map... Daniel@0: Daniel@0: clc Daniel@0: % VISUAL INSPECTION OF THE MAP Daniel@0: % ============================ Daniel@0: Daniel@0: % The first step in the analysis of the map is visual inspection. Daniel@0: % Here is the U-matrix, component planes and labels (you may Daniel@0: % need to enlarge the figure in order to make out the labels). Daniel@0: Daniel@0: som_show(sM,'umat','all','comp',[1:4],'empty','Labels','norm','d'); Daniel@0: som_show_add('label',sM.labels,'textsize',8,'textcolor','r','subplot',6); Daniel@0: Daniel@0: % From this first visualization, one can see that: Daniel@0: % - there are essentially two clusters Daniel@0: % - PetalL and PetalW are highly correlated Daniel@0: % - SepalL is somewhat correlated to PetalL and PetalW Daniel@0: % - one cluster corresponds to the Setosa species and exhibits Daniel@0: % small petals and short but wide sepals Daniel@0: % - the other cluster corresponds to Virginica and Versicolor Daniel@0: % such that Versicolor has smaller leaves (both sepal and Daniel@0: % petal) than Virginica Daniel@0: % - inside both clusters, SepalL and SepalW are highly correlated Daniel@0: Daniel@0: pause % Strike any key to continue... Daniel@0: Daniel@0: % Next, the projection of the data set is investigated. A Daniel@0: % principle component projection is made for the data, and applied Daniel@0: % to the map. The colormap is done by spreading a colormap on the Daniel@0: % projection. Distance matrix information is extracted from the Daniel@0: % U-matrix, and it is modified by knowledge of zero-hits Daniel@0: % (interpolative) units. Finally, three visualizations are shown: Daniel@0: % the color code, with clustering information and the number of Daniel@0: % hits in each unit, the projection and the labels. Daniel@0: Daniel@0: echo off Daniel@0: Daniel@0: f1=figure; Daniel@0: [Pd,V,me,l] = pcaproj(sD,2); Pm = pcaproj(sM,V,me); % PC-projection Daniel@0: Code = som_colorcode(Pm); % color coding Daniel@0: hits = som_hits(sM,sD); % hits Daniel@0: U = som_umat(sM); % U-matrix Daniel@0: Dm = U(1:2:size(U,1),1:2:size(U,2)); % distance matrix Daniel@0: Dm = 1-Dm(:)/max(Dm(:)); Dm(find(hits==0)) = 0; % clustering info Daniel@0: Daniel@0: subplot(1,3,1) Daniel@0: som_cplane(sM,Code,Dm); Daniel@0: hold on Daniel@0: som_grid(sM,'Label',cellstr(int2str(hits)),... Daniel@0: 'Line','none','Marker','none','Labelcolor','k'); Daniel@0: hold off Daniel@0: title('Color code') Daniel@0: Daniel@0: subplot(1,3,2) Daniel@0: som_grid(sM,'Coord',Pm,'MarkerColor',Code,'Linecolor','k'); Daniel@0: hold on, plot(Pd(:,1),Pd(:,2),'k+'), hold off, axis tight, axis equal Daniel@0: title('PC projection') Daniel@0: Daniel@0: subplot(1,3,3) Daniel@0: som_cplane(sM,'none') Daniel@0: hold on Daniel@0: som_grid(sM,'Label',sM.labels,'Labelsize',8,... Daniel@0: 'Line','none','Marker','none','Labelcolor','r'); Daniel@0: hold off Daniel@0: title('Labels') Daniel@0: Daniel@0: echo on Daniel@0: Daniel@0: % From these figures one can see that: Daniel@0: % - the projection confirms the existence of two different clusters Daniel@0: % - interpolative units seem to divide the Virginica Daniel@0: % flowers into two classes, the difference being in the size of Daniel@0: % sepal leaves Daniel@0: Daniel@0: pause % Strike any key to continue... Daniel@0: Daniel@0: % Finally, perhaps the most informative figure of all: simple Daniel@0: % scatter plots and histograms of all variables. The species Daniel@0: % information is coded as a fifth variable: 1 for Setosa, 2 for Daniel@0: % Versicolor and 3 for Virginica. Original data points are in the Daniel@0: % upper triangle, map prototype values on the lower triangle, and Daniel@0: % histograms on the diagonal: black for the data set and red for Daniel@0: % the map prototype values. The color coding of the data samples Daniel@0: % has been copied from the map (from the BMU of each sample). Note Daniel@0: % that the variable values have been denormalized. Daniel@0: Daniel@0: echo off Daniel@0: Daniel@0: % denormalize and add species information Daniel@0: names = sD.comp_names; names{end+1} = 'species'; Daniel@0: D = som_denormalize(sD.data,sD); dlen = size(D,1); Daniel@0: s = zeros(dlen,1)+NaN; s(strcmp(sD.labels,'Setosa'))=1; Daniel@0: s(strcmp(sD.labels,'Versicolor'))=2; s(strcmp(sD.labels,'Virginica'))=3; Daniel@0: D = [D, s]; Daniel@0: M = som_denormalize(sM.codebook,sM); munits = size(M,1); Daniel@0: s = zeros(munits,1)+NaN; s(strcmp(sM.labels,'Setosa'))=1; Daniel@0: s(strcmp(sM.labels,'Versicolor'))=2; s(strcmp(sM.labels,'Virginica'))=3; Daniel@0: M = [M, s]; Daniel@0: Daniel@0: f2=figure; Daniel@0: Daniel@0: % color coding copied from the map Daniel@0: bmus = som_bmus(sM,sD); Code_data = Code(bmus,:); Daniel@0: Daniel@0: k=1; Daniel@0: for i=1:5, for j=1:5, Daniel@0: if ij, Daniel@0: som_grid(sM,'coord',M(:,[i1 i2]),... Daniel@0: 'markersize',2,'MarkerColor',Code); Daniel@0: title(sprintf('%s vs. %s',names{i1},names{i2})) Daniel@0: else Daniel@0: if i1<5, b = 10; else b = 3; end Daniel@0: [nd,x] = hist(D(:,i1),b); nd=nd/sum(nd); Daniel@0: nm = hist(M(:,i1),x); nm = nm/sum(nm); Daniel@0: h=bar(x,nd,0.8); set(h,'EdgeColor','none','FaceColor','k'); Daniel@0: hold on Daniel@0: h=bar(x,nm,0.3); set(h,'EdgeColor','none','FaceColor','r'); Daniel@0: hold off Daniel@0: title(names{i1}) Daniel@0: end Daniel@0: k=k+1; Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: echo on Daniel@0: Daniel@0: % This visualization shows quite a lot of information: Daniel@0: % distributions of single and pairs of variables both in the data Daniel@0: % and in the map. If the number of variables was even slightly Daniel@0: % more, it would require a really big display to be convenient to Daniel@0: % use. Daniel@0: Daniel@0: % From this visualization we can conform many of the earlier Daniel@0: % conclusions, for example: Daniel@0: % - there are two clusters: 'Setosa' (blue, dark green) and Daniel@0: % 'Virginica'/'Versicolor' (light green, yellow, reds) Daniel@0: % (see almost any of the subplots) Daniel@0: % - PetalL and PetalW have a high linear correlation (see Daniel@0: % subplots 4,3 and 3,4) Daniel@0: % - SepalL is correlated (at least in the bigger cluster) with Daniel@0: % PetalL and PetalW (in subplots 1,3 1,4 3,1 and 4,1) Daniel@0: % - SepalL and SepalW have a clear linear correlation, but it Daniel@0: % is slightly different for the two main clusters (in Daniel@0: % subplots 2,1 and 1,2) Daniel@0: Daniel@0: pause % Strike any key to cluster the map... Daniel@0: Daniel@0: close(f1), close(f2), figure(f0), clf Daniel@0: Daniel@0: clc Daniel@0: % CLUSTERING OF THE MAP Daniel@0: % ===================== Daniel@0: Daniel@0: % Visual inspection already hinted that there are at least two Daniel@0: % clusters in the data, and that the properties of the clusters are Daniel@0: % different from each other (esp. relation of SepalL and Daniel@0: % SepalW). For further investigation, the map needs to be Daniel@0: % partitioned. Daniel@0: Daniel@0: % Here, the KMEANS_CLUSTERS function is used to find an initial Daniel@0: % partitioning. The plot shows the Davies-Boulding clustering Daniel@0: % index, which is minimized with best clustering. Daniel@0: Daniel@0: subplot(1,3,1) Daniel@0: [c,p,err,ind] = kmeans_clusters(sM, 7); % find at most 7 clusters Daniel@0: plot(1:length(ind),ind,'x-') Daniel@0: [dummy,i] = min(ind) Daniel@0: cl = p{i}; Daniel@0: Daniel@0: % The Davies-Boulding index seems to indicate that there are Daniel@0: % two clusters on the map. Here is the clustering info Daniel@0: % calculated previously and the partitioning result: Daniel@0: Daniel@0: subplot(1,3,2) Daniel@0: som_cplane(sM,Code,Dm) Daniel@0: subplot(1,3,3) Daniel@0: som_cplane(sM,cl) Daniel@0: Daniel@0: % You could use also function SOM_SELECT to manually make or modify Daniel@0: % the partitioning. Daniel@0: Daniel@0: % After this, the analysis would proceed with summarization of the Daniel@0: % results, and analysis of each cluster one at a time. Daniel@0: % Unfortunately, you have to do that yourself. The SOM Toolbox does Daniel@0: % not, yet, have functions for those purposes. Daniel@0: Daniel@0: pause % Strike any key to continue... Daniel@0: Daniel@0: Daniel@0: clf Daniel@0: clc Daniel@0: % MODELING Daniel@0: % ======== Daniel@0: Daniel@0: % One can also build models on top of the SOM. Typically, these Daniel@0: % models are simple local or nearest-neighbor models. Daniel@0: Daniel@0: % Here, SOM is used for probability density estimation. Each map Daniel@0: % prototype is the center of a gaussian kernel, the parameters Daniel@0: % of which are estimated from the data. The gaussian mixture Daniel@0: % model is estimated with function SOM_ESTIMATE_GMM and the Daniel@0: % probabilities can be calculated with SOM_PROBABILITY_GMM. Daniel@0: Daniel@0: [K,P] = som_estimate_gmm(sM,sD); Daniel@0: [pd,Pdm,pmd] = som_probability_gmm(sD,sM,K,P); Daniel@0: Daniel@0: % Here is the probability density function value for the first data Daniel@0: % sample (x=sD.data(:,1)) in terms of each map unit (m): Daniel@0: Daniel@0: som_cplane(sM,Pdm(:,1)) Daniel@0: colorbar Daniel@0: title('p(x|m)') Daniel@0: Daniel@0: pause % Strike any key to continue... Daniel@0: Daniel@0: % Here, SOM is used for classification. Although the SOM can be Daniel@0: % used for classification as such, one has to remember that it does Daniel@0: % not utilize class information at all, and thus its results are Daniel@0: % inherently suboptimal. However, with small modifications, the Daniel@0: % network can take the class into account. The function Daniel@0: % SOM_SUPERVISED does this. Daniel@0: Daniel@0: % Learning vector quantization (LVQ) is an algorithm that is very Daniel@0: % similar to the SOM in many aspects. However, it is specifically Daniel@0: % designed for classification. In the SOM Toolbox, there are Daniel@0: % functions LVQ1 and LVQ3 that implement two versions of this Daniel@0: % algorithm. Daniel@0: Daniel@0: % Here, the function SOM_SUPERVISED is used to create a classifier Daniel@0: % for IRIS data set: Daniel@0: Daniel@0: sM = som_supervised(sD,'small'); Daniel@0: Daniel@0: som_show(sM,'umat','all'); Daniel@0: som_show_add('label',sM.labels,'TextSize',8,'TextColor','r') Daniel@0: Daniel@0: sD2 = som_label(sD,'clear','all'); Daniel@0: sD2 = som_autolabel(sD2,sM); % classification Daniel@0: ok = strcmp(sD2.labels,sD.labels); % errors Daniel@0: 100*(1-sum(ok)/length(ok)) % error percentage (%) Daniel@0: Daniel@0: echo off Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: