wolffd@0: wolffd@0: %SOM_DEMO3 Self-organizing map visualization. wolffd@0: wolffd@0: % Contributed to SOM Toolbox 2.0, February 11th, 2000 by Juha Vesanto wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 1.0beta juuso 071197 wolffd@0: % Version 2.0beta juuso 080200 070600 wolffd@0: wolffd@0: clf reset; wolffd@0: figure(gcf) wolffd@0: echo on wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: % ========================================================== wolffd@0: % SOM_DEMO3 - VISUALIZATION wolffd@0: % ========================================================== wolffd@0: wolffd@0: % som_show - Visualize map. wolffd@0: % som_grid - Visualization with free coordinates. wolffd@0: % wolffd@0: % som_show_add - Add markers on som_show visualization. wolffd@0: % som_show_clear - Remove markers from som_show visualization. wolffd@0: % som_recolorbar - Refresh and rescale colorbars in som_show wolffd@0: % visualization. wolffd@0: % wolffd@0: % som_cplane - Visualize component/color/U-matrix plane. wolffd@0: % som_pieplane - Visualize prototype vectors as pie charts. wolffd@0: % som_barplane - Visualize prototype vectors as bar charts. wolffd@0: % som_plotplane - Visualize prototype vectors as line graphs. wolffd@0: % wolffd@0: % pcaproj - Projection to principal component space. wolffd@0: % cca - Projection with Curvilinear Component Analysis. wolffd@0: % sammon - Projection with Sammon's mapping. wolffd@0: % som_umat - Calculate U-matrix. wolffd@0: % som_colorcode - Color coding for the map. wolffd@0: % som_normcolor - RGB values of indexed colors. wolffd@0: % som_hits - Hit histograms for the map. wolffd@0: wolffd@0: % The basic functions for SOM visualization are SOM_SHOW and wolffd@0: % SOM_GRID. The SOM_SHOW has three auxiliary functions: wolffd@0: % SOM_SHOW_ADD, SOM_SHOW_CLEAR and SOM_RECOLORBAR which are used wolffd@0: % to add and remove markers and to control the colorbars. wolffd@0: % SOM_SHOW actually uses SOM_CPLANE to make the visualizations. wolffd@0: % Also SOM_{PIE,BAR,PLOT}PLANE can be used to visualize SOMs. wolffd@0: wolffd@0: % The other functions listed above do not themselves visualize wolffd@0: % anything, but their results are used in the visualizations. wolffd@0: wolffd@0: % There's an important limitation that visualization functions have: wolffd@0: % while the SOM Toolbox otherwise supports N-dimensional map grids, wolffd@0: % visualization only works for 1- and 2-dimensional map grids!!! wolffd@0: wolffd@0: pause % Strike any key to create demo data and map... wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: % DEMO DATA AND MAP wolffd@0: % ================= wolffd@0: wolffd@0: % The data set contructed for this demo consists of random vectors wolffd@0: % in three gaussian kernels the centers of which are at [0, 0, 0], wolffd@0: % [3 3 3] and [9 0 0]. The map is trained using default parameters. wolffd@0: wolffd@0: D1 = randn(100,3); wolffd@0: D2 = randn(100,3) + 3; wolffd@0: D3 = randn(100,3); D3(:,1) = D3(:,1) + 9; wolffd@0: wolffd@0: sD = som_data_struct([D1; D2; D3],'name','Demo3 data',... wolffd@0: 'comp_names',{'X-coord','Y-coord','Z-coord'}); wolffd@0: sM = som_make(sD); wolffd@0: wolffd@0: % Since the data (and thus the prototypes of the map) are wolffd@0: % 3-dimensional, they can be directly plotted using PLOT3. wolffd@0: % Below, the data is plotted using red 'o's and the map wolffd@0: % prototype vectors with black '+'s. wolffd@0: wolffd@0: plot3(sD.data(:,1),sD.data(:,2),sD.data(:,3),'ro',... wolffd@0: sM.codebook(:,1),sM.codebook(:,2),sM.codebook(:,3),'k+') wolffd@0: rotate3d on wolffd@0: wolffd@0: % From the visualization it is pretty easy to see what the data is wolffd@0: % like, and how the prototypes have been positioned. One can see wolffd@0: % that there are three clusters, and that there are some prototype wolffd@0: % vectors between the clusters, although there is actually no wolffd@0: % data there. The map units corresponding to these prototypes wolffd@0: % are called 'dead' or 'interpolative' map units. wolffd@0: wolffd@0: pause % Strike any key to continue... wolffd@0: wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: % VISUALIZATION OF MULTIDIMENSIONAL DATA wolffd@0: % ====================================== wolffd@0: wolffd@0: % Usually visualization of data sets is not this straightforward, wolffd@0: % since the dimensionality is much higher than three. In principle, wolffd@0: % one can embed additional information to the visualization by wolffd@0: % using properties other than position, for example color, size or wolffd@0: % shape. wolffd@0: wolffd@0: % Here the data set and map prototypes are plotted again, but wolffd@0: % information of the cluster is shown using color: red for the wolffd@0: % first cluster, green for the second and blue for the last. wolffd@0: wolffd@0: plot3(sD.data(1:100,1),sD.data(1:100,2),sD.data(1:100,3),'ro',... wolffd@0: sD.data(101:200,1),sD.data(101:200,2),sD.data(101:200,3),'go',... wolffd@0: sD.data(201:300,1),sD.data(201:300,2),sD.data(201:300,3),'bo',... wolffd@0: sM.codebook(:,1),sM.codebook(:,2),sM.codebook(:,3),'k+') wolffd@0: rotate3d on wolffd@0: wolffd@0: % However, this works only for relatively small dimensionality, say wolffd@0: % less than 10. When the information is added this way, the wolffd@0: % visualization becomes harder and harder to understand. Also, not wolffd@0: % all properties are equal: the human visual system perceives wolffd@0: % colors differently from position, not to mention the complex wolffd@0: % rules governing perception of shape. wolffd@0: wolffd@0: pause % Strike any key to learn about linking... wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: % LINKING MULTIPLE VISUALIZATIONS wolffd@0: % =============================== wolffd@0: wolffd@0: % The other option is to use *multiple visualizations*, so called wolffd@0: % small multiples, instead of only one. The problem is then how to wolffd@0: % link these visualizations together: one should be able to idetify wolffd@0: % the same object from the different visualizations. wolffd@0: wolffd@0: % This could be done using, for example, color: each object has wolffd@0: % the same color in each visualization. Another option is to use wolffd@0: % similar position: each object has the same position in each wolffd@0: % small multiple. wolffd@0: wolffd@0: % For example, here are four subplots, one for each component and wolffd@0: % one for cluster information, where color denotes the value and wolffd@0: % position is used for linking. The 2D-position is derived by wolffd@0: % projecting the data into the space spanned by its two greatest wolffd@0: % eigenvectors. wolffd@0: wolffd@0: [Pd,V,me] = pcaproj(sD.data,2); % project the data wolffd@0: Pm = pcaproj(sM.codebook,V,me); % project the prototypes wolffd@0: colormap(hot); % colormap used for values wolffd@0: wolffd@0: echo off wolffd@0: for c=1:3, wolffd@0: subplot(2,2,c), cla, hold on wolffd@0: som_grid('rect',[300 1],'coord',Pd,'Line','none',... wolffd@0: 'MarkerColor',som_normcolor(sD.data(:,c))); wolffd@0: som_grid(sM,'Coord',Pm,'Line','none','marker','+'); wolffd@0: hold off, title(sD.comp_names{c}), xlabel('PC 1'), ylabel('PC 2'); wolffd@0: end wolffd@0: wolffd@0: subplot(2,2,4), cla wolffd@0: plot(Pd(1:100,1),Pd(1:100,2),'ro',... wolffd@0: Pd(101:200,1),Pd(101:200,2),'go',... wolffd@0: Pd(201:300,1),Pd(201:300,2),'bo',... wolffd@0: Pm(:,1),Pm(:,2),'k+') wolffd@0: title('Cluster') wolffd@0: echo on wolffd@0: wolffd@0: pause % Strike any key to use color for linking... wolffd@0: wolffd@0: % Here is another example, where color is used for linking. On the wolffd@0: % top right triangle are the scatter plots of each variable without wolffd@0: % color coding, and on the bottom left triangle with the color wolffd@0: % coding. In the colored figures, each data sample can be wolffd@0: % identified by a unique color. Well, almost identified: there are wolffd@0: % quite a lot of samples with almost the same color. Color is not as wolffd@0: % precise linking method as position. wolffd@0: wolffd@0: echo off wolffd@0: Col = som_normcolor([1:300]',jet(300)); wolffd@0: k=1; wolffd@0: for i=1:3, wolffd@0: for j=1:3, wolffd@0: if ij, wolffd@0: subplot(3,3,k); cla wolffd@0: som_grid('rect',[300 1],'coord',sD.data(:,[i1 i2]),... wolffd@0: 'Line','none','MarkerColor',Col); wolffd@0: xlabel(sD.comp_names{i1}), ylabel(sD.comp_names{i2}) wolffd@0: end wolffd@0: k=k+1; wolffd@0: end wolffd@0: end wolffd@0: echo on wolffd@0: wolffd@0: pause % Strike any key to learn about data visualization using SOM... wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: % DATA VISUALIZATION USING SOM wolffd@0: % ============================ wolffd@0: wolffd@0: % The basic visualization functions and their usage have already wolffd@0: % been introduced in SOM_DEMO2. In this demo, a more structured wolffd@0: % presentation is given. wolffd@0: wolffd@0: % Data visualization techniques using the SOM can be divided to wolffd@0: % three categories based on their goal: wolffd@0: wolffd@0: % 1. visualization of clusters and shape of the data: wolffd@0: % projections, U-matrices and other distance matrices wolffd@0: % wolffd@0: % 2. visualization of components / variables: wolffd@0: % component planes, scatter plots wolffd@0: % wolffd@0: % 3. visualization of data projections: wolffd@0: % hit histograms, response surfaces wolffd@0: wolffd@0: pause % Strike any key to visualize clusters with distance matrices... wolffd@0: wolffd@0: wolffd@0: wolffd@0: clf wolffd@0: clc wolffd@0: % 1. VISUALIZATION OF CLUSTERS: DISTANCE MATRICES wolffd@0: % =============================================== wolffd@0: wolffd@0: % Distance matrices are typically used to show the cluster wolffd@0: % structure of the SOM. They show distances between neighboring wolffd@0: % units, and are thus closely related to single linkage clustering wolffd@0: % techniques. The most widely used distance matrix technique is wolffd@0: % the U-matrix. wolffd@0: wolffd@0: % Here, the U-matrix of the map is shown (using all three wolffd@0: % components in the distance calculation): wolffd@0: wolffd@0: colormap(1-gray) wolffd@0: som_show(sM,'umat','all'); wolffd@0: wolffd@0: pause % Strike any key to see more examples of distance matrices... wolffd@0: wolffd@0: % The function SOM_UMAT can be used to calculate U-matrix. The wolffd@0: % resulting matrix holds distances between neighboring map units, wolffd@0: % as well as the median distance from each map unit to its wolffd@0: % neighbors. These median distances corresponding to each map unit wolffd@0: % can be easily extracted. The result is a distance matrix using wolffd@0: % median distance. wolffd@0: wolffd@0: U = som_umat(sM); wolffd@0: Um = U(1:2:size(U,1),1:2:size(U,2)); wolffd@0: wolffd@0: % A related technique is to assign colors to the map units such wolffd@0: % that similar map units get similar colors. wolffd@0: wolffd@0: % Here, four clustering figures are shown: wolffd@0: % - U-matrix wolffd@0: % - median distance matrix (with grayscale) wolffd@0: % - median distance matrix (with map unit size) wolffd@0: % - similarity coloring, made by spreading a colormap wolffd@0: % on top of the principal component projection of the wolffd@0: % prototype vectors wolffd@0: wolffd@0: subplot(2,2,1) wolffd@0: h=som_cplane([sM.topol.lattice,'U'],sM.topol.msize, U(:)); wolffd@0: set(h,'Edgecolor','none'); title('U-matrix') wolffd@0: wolffd@0: subplot(2,2,2) wolffd@0: h=som_cplane(sM, Um(:)); wolffd@0: set(h,'Edgecolor','none'); title('D-matrix (grayscale)') wolffd@0: wolffd@0: subplot(2,2,3) wolffd@0: som_cplane(sM,'none',1-Um(:)/max(Um(:))) wolffd@0: title('D-matrix (marker size)') wolffd@0: wolffd@0: subplot(2,2,4) wolffd@0: C = som_colorcode(Pm); % Pm is the PC-projection calculated earlier wolffd@0: som_cplane(sM,C) wolffd@0: title('Similarity coloring') wolffd@0: wolffd@0: pause % Strike any key to visualize shape and clusters with projections... wolffd@0: wolffd@0: wolffd@0: wolffd@0: clf wolffd@0: clc wolffd@0: % 1. VISUALIZATION OF CLUSTERS AND SHAPE: PROJECTIONS wolffd@0: % =================================================== wolffd@0: wolffd@0: % In vector projection, a set of high-dimensional data samples is wolffd@0: % projected to a lower dimensional such that the distances between wolffd@0: % data sample pairs are preserved as well as possible. Depending wolffd@0: % on the technique, the projection may be either linear or wolffd@0: % non-linear, and it may place special emphasis on preserving wolffd@0: % local distances. wolffd@0: wolffd@0: % For example SOM is a projection technique, since the prototypes wolffd@0: % have well-defined positions on the 2-dimensional map grid. SOM as wolffd@0: % a projection is however a very crude one. Other projection wolffd@0: % techniques include the principal component projection used wolffd@0: % earlier, Sammon's mapping and Curvilinear Component Analysis wolffd@0: % (to name a few). These have been implemented in functions wolffd@0: % PCAPROJ, SAMMON and CCA. wolffd@0: wolffd@0: % Projecting the map prototype vectors and joining neighboring map wolffd@0: % units with lines gives the SOM its characteristic net-like look. wolffd@0: % The projection figures can be linked to the map planes using wolffd@0: % color coding. wolffd@0: wolffd@0: % Here is the distance matrix, color coding, a projection without wolffd@0: % coloring and a projection with one. In the last projection, wolffd@0: % the size of interpolating map units has been set to zero. wolffd@0: wolffd@0: subplot(2,2,1) wolffd@0: som_cplane(sM,Um(:)); wolffd@0: title('Distance matrix') wolffd@0: wolffd@0: subplot(2,2,2) wolffd@0: C = som_colorcode(sM,'rgb4'); wolffd@0: som_cplane(sM,C); wolffd@0: title('Color code') wolffd@0: wolffd@0: subplot(2,2,3) wolffd@0: som_grid(sM,'Coord',Pm,'Linecolor','k'); wolffd@0: title('PC-projection') wolffd@0: wolffd@0: subplot(2,2,4) wolffd@0: h = som_hits(sM,sD); s=6*(h>0); wolffd@0: som_grid(sM,'Coord',Pm,'MarkerColor',C,'Linecolor','k','MarkerSize',s); wolffd@0: title('Colored PC-projection') wolffd@0: wolffd@0: pause % Strike any key to visualize component planes... wolffd@0: wolffd@0: wolffd@0: clf wolffd@0: clc wolffd@0: % 2. VISUALIZATION OF COMPONENTS: COMPONENT PLANES wolffd@0: % ================================================ wolffd@0: wolffd@0: % The component planes visualizations shows what kind of values the wolffd@0: % prototype vectors of the map units have for different vector wolffd@0: % components. wolffd@0: wolffd@0: % Here is the U-matrix and the three component planes of the map. wolffd@0: wolffd@0: som_show(sM) wolffd@0: wolffd@0: pause % Strike any key to continue... wolffd@0: wolffd@0: % Besides SOM_SHOW and SOM_CPLANE, there are three other wolffd@0: % functions specifically designed for showing the values of the wolffd@0: % component planes: SOM_PIEPLANE, SOM_BARPLANE, SOM_PLOTPLANE. wolffd@0: wolffd@0: % SOM_PIEPLANE shows a single pie chart for each map unit. Each wolffd@0: % pie shows the relative proportion of each component of the sum of wolffd@0: % all components in that map unit. The component values must be wolffd@0: % positive. wolffd@0: wolffd@0: % SOM_BARPLANE shows a barchart in each map unit. The scaling of wolffd@0: % bars can be made unit-wise or variable-wise. By default it is wolffd@0: % determined variable-wise. wolffd@0: wolffd@0: % SOM_PLOTPLANE shows a linegraph in each map unit. wolffd@0: wolffd@0: M = som_normalize(sM.codebook,'range'); wolffd@0: wolffd@0: subplot(1,3,1) wolffd@0: som_pieplane(sM, M); wolffd@0: title('som\_pieplane') wolffd@0: wolffd@0: subplot(1,3,2) wolffd@0: som_barplane(sM, M, '', 'unitwise'); wolffd@0: title('som\_barplane') wolffd@0: wolffd@0: subplot(1,3,3) wolffd@0: som_plotplane(sM, M, 'b'); wolffd@0: title('som\_plotplane') wolffd@0: wolffd@0: pause % Strike any key to visualize cluster properties... wolffd@0: wolffd@0: wolffd@0: wolffd@0: clf wolffd@0: clc wolffd@0: % 2. VISUALIZATION OF COMPONENTS: CLUSTERS wolffd@0: % ======================================== wolffd@0: wolffd@0: % An interesting question is of course how do the values of the wolffd@0: % variables relate to the clusters: what are the values of the wolffd@0: % components in the clusters, and which components are the ones wolffd@0: % which *make* the clusters. wolffd@0: wolffd@0: som_show(sM) wolffd@0: wolffd@0: % From the U-matrix and component planes, one can easily see wolffd@0: % what the typical values are in each cluster. wolffd@0: wolffd@0: pause % Strike any key to continue... wolffd@0: wolffd@0: % The significance of the components with respect to the clustering wolffd@0: % is harder to visualize. One indication of importance is that on wolffd@0: % the borders of the clusters, values of important variables change wolffd@0: % very rabidly. wolffd@0: wolffd@0: % Here, the distance matrix is calculated with respect to each wolffd@0: % variable. wolffd@0: wolffd@0: u1 = som_umat(sM,'mask',[1 0 0]'); u1=u1(1:2:size(u1,1),1:2:size(u1,2)); wolffd@0: u2 = som_umat(sM,'mask',[0 1 0]'); u2=u2(1:2:size(u2,1),1:2:size(u2,2)); wolffd@0: u3 = som_umat(sM,'mask',[0 0 1]'); u3=u3(1:2:size(u3,1),1:2:size(u3,2)); wolffd@0: wolffd@0: % Here, the distance matrices are shown, as well as a piechart wolffd@0: % indicating the relative importance of each variable in each wolffd@0: % map unit. The size of piecharts has been scaled by the wolffd@0: % distance matrix calculated from all components. wolffd@0: wolffd@0: subplot(2,2,1) wolffd@0: som_cplane(sM,u1(:)); wolffd@0: title(sM.comp_names{1}) wolffd@0: wolffd@0: subplot(2,2,2) wolffd@0: som_cplane(sM,u2(:)); wolffd@0: title(sM.comp_names{2}) wolffd@0: wolffd@0: subplot(2,2,3) wolffd@0: som_cplane(sM,u3(:)); wolffd@0: title(sM.comp_names{3}) wolffd@0: wolffd@0: subplot(2,2,4) wolffd@0: som_pieplane(sM, [u1(:), u2(:), u3(:)], hsv(3), Um(:)/max(Um(:))); wolffd@0: title('Relative importance') wolffd@0: wolffd@0: % From the last subplot, one can see that in the area where the wolffd@0: % bigger cluster border is, the 'X-coord' component (red color) wolffd@0: % has biggest effect, and thus is the main factor in separating wolffd@0: % that cluster from the rest. wolffd@0: wolffd@0: pause % Strike any key to learn about correlation hunting... wolffd@0: wolffd@0: wolffd@0: clf wolffd@0: clc wolffd@0: % 2. VISUALIZATION OF COMPONENTS: CORRELATION HUNTING wolffd@0: % =================================================== wolffd@0: wolffd@0: % Finally, the component planes are often used for correlation wolffd@0: % hunting. When the number of variables is high, the component wolffd@0: % plane visualization offers a convenient way to visualize all wolffd@0: % components at once and hunt for correlations (as opposed to wolffd@0: % N*(N-1)/2 scatterplots). wolffd@0: wolffd@0: % Hunting correlations this way is not very accurate. However, it wolffd@0: % is easy to select interesting combinations for further wolffd@0: % investigation. wolffd@0: wolffd@0: % Here, the first and third components are shown with scatter wolffd@0: % plot. As with projections, a color coding is used to link the wolffd@0: % visualization to the map plane. In the color coding, size shows wolffd@0: % the distance matrix information. wolffd@0: wolffd@0: C = som_colorcode(sM); wolffd@0: subplot(1,2,1) wolffd@0: som_cplane(sM,C,1-Um(:)/max(Um(:))); wolffd@0: title('Color coding + distance matrix') wolffd@0: wolffd@0: subplot(1,2,2) wolffd@0: som_grid(sM,'Coord',sM.codebook(:,[1 3]),'MarkerColor',C); wolffd@0: title('Scatter plot'); xlabel(sM.comp_names{1}); ylabel(sM.comp_names{3}) wolffd@0: axis equal wolffd@0: wolffd@0: pause % Strike any key to visualize data responses... wolffd@0: wolffd@0: wolffd@0: clf wolffd@0: clc wolffd@0: % 3. DATA ON MAP wolffd@0: % ============== wolffd@0: wolffd@0: % The SOM is a map of the data manifold. An interesting question wolffd@0: % then is where on the map a specific data sample is located, and wolffd@0: % how accurate is that localization? One is interested in the wolffd@0: % response of the map to the data sample. wolffd@0: wolffd@0: % The simplest answer is to find the BMU of the data sample. wolffd@0: % However, this gives no indication of the accuracy of the wolffd@0: % match. Is the data sample close to the BMU, or is it actually wolffd@0: % equally close to the neighboring map units (or even approximately wolffd@0: % as close to all map units)? Sometimes accuracy doesn't really wolffd@0: % matter, but if it does, it should be visualized somehow. wolffd@0: wolffd@0: % Here are different kinds of response visualizations for two wolffd@0: % vectors: [0 0 0] and [99 99 99]. wolffd@0: % - BMUs indicated with labels wolffd@0: % - BMUs indicated with markers, relative quantization errors wolffd@0: % (in this case, proportion between distances to BMU and wolffd@0: % Worst-MU) with vertical lines wolffd@0: % - quantization error between the samples and all map units wolffd@0: % - fuzzy response (a non-linear function of quantization wolffd@0: % error) of all map units wolffd@0: wolffd@0: echo off wolffd@0: [bm,qe] = som_bmus(sM,[0 0 0; 99 99 99],'all'); % distance to all map units wolffd@0: [dummy,ind] = sort(bm(1,:)); d0 = qe(1,ind)'; wolffd@0: [dummy,ind] = sort(bm(2,:)); d9 = qe(2,ind)'; wolffd@0: bmu0 = bm(1,1); bmu9 = bm(2,1); % bmus wolffd@0: wolffd@0: h0 = zeros(prod(sM.topol.msize),1); h0(bmu0) = 1; % crisp hits wolffd@0: h9 = zeros(prod(sM.topol.msize),1); h9(bmu9) = 1; wolffd@0: wolffd@0: lab = cell(prod(sM.topol.msize),1); wolffd@0: lab{bmu0} = '[0,0,0]'; lab{bmu9} = '[99,99,99]'; wolffd@0: wolffd@0: hf0 = som_hits(sM,[0 0 0],'fuzzy'); % fuzzy response wolffd@0: hf9 = som_hits(sM,[99 99 99],'fuzzy'); wolffd@0: wolffd@0: som_show(sM,'umat',{'all','BMU'},... wolffd@0: 'color',{d0,'Qerror 0'},'color',{hf0,'Fuzzy response 0'},... wolffd@0: 'empty','BMU+qerror',... wolffd@0: 'color',{d9,'Qerror 99'},'color',{hf9,'Fuzzy response 99'}); wolffd@0: som_show_add('label',lab,'Subplot',1,'Textcolor','r'); wolffd@0: som_show_add('hit',[h0, h9],'Subplot',4,'MarkerColor','r'); wolffd@0: hold on wolffd@0: Co = som_vis_coords(sM.topol.lattice,sM.topol.msize); wolffd@0: plot3(Co(bmu0,[1 1]),Co(bmu0,[2 2]),[0 10*qe(1,1)/qe(1,end)],'r-') wolffd@0: plot3(Co(bmu9,[1 1]),Co(bmu9,[2 2]),[0 10*qe(2,1)/qe(2,end)],'r-') wolffd@0: view(3), axis equal wolffd@0: echo on wolffd@0: wolffd@0: % Here are the distances to BMU, 2-BMU and WMU: wolffd@0: wolffd@0: qe(1,[1,2,end]) % [0 0 0] wolffd@0: qe(2,[1,2,end]) % [99 99 99] wolffd@0: wolffd@0: % One can see that for [0 0 0] the accuracy is pretty good as the wolffd@0: % quantization error of the BMU is much lower than that of the wolffd@0: % WMU. On the other hand [99 99 99] is very far from the map: wolffd@0: % distance to BMU is almost equal to distance to WMU. wolffd@0: wolffd@0: pause % Strike any key to visualize responses of multiple samples... wolffd@0: wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: clf wolffd@0: % 3. DATA ON MAP: HIT HISTOGRAMS wolffd@0: % ============================== wolffd@0: wolffd@0: % One can also investigate whole data sets using the map. When the wolffd@0: % BMUs of multiple data samples are aggregated, a hit histogram wolffd@0: % results. Instead of BMUs, one can also aggregate for example wolffd@0: % fuzzy responses. wolffd@0: wolffd@0: % The hit histograms (or aggregated responses) can then be compared wolffd@0: % with each other. wolffd@0: wolffd@0: % Here are hit histograms of three data sets: one with 50 first wolffd@0: % vectors of the data set, one with 150 samples from the data wolffd@0: % set, and one with 50 randomly selected samples. In the last wolffd@0: % subplot, the fuzzy response of the first data set. wolffd@0: wolffd@0: dlen = size(sD.data,1); wolffd@0: Dsample1 = sD.data(1:50,:); h1 = som_hits(sM,Dsample1); wolffd@0: Dsample2 = sD.data(1:150,:); h2 = som_hits(sM,Dsample2); wolffd@0: Dsample3 = sD.data(ceil(rand(50,1)*dlen),:); h3 = som_hits(sM,Dsample3); wolffd@0: hf = som_hits(sM,Dsample1,'fuzzy'); wolffd@0: wolffd@0: som_show(sM,'umat','all','umat','all','umat','all','color',{hf,'Fuzzy'}) wolffd@0: som_show_add('hit',h1,'Subplot',1,'Markercolor','r') wolffd@0: som_show_add('hit',h2,'Subplot',2,'Markercolor','r') wolffd@0: som_show_add('hit',h3,'Subplot',3,'Markercolor','r') wolffd@0: wolffd@0: pause % Strike any key to visualize trajectories... wolffd@0: wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: clf wolffd@0: % 3. DATA ON MAP: TRAJECTORIES wolffd@0: % ============================ wolffd@0: wolffd@0: % A special data mapping technique is trajectory. If the samples wolffd@0: % are ordered, forming a time-series for example, their response on wolffd@0: % the map can be tracked. The function SOM_SHOW_ADD can be used to wolffd@0: % show the trajectories in two different modes: 'traj' and 'comet'. wolffd@0: wolffd@0: % Here, a series of data points is formed which go from [8,0,0] wolffd@0: % to [2,2,2]. The trajectory is plotted using the two modes. wolffd@0: wolffd@0: Dtraj = [linspace(9,2,20); linspace(0,2,20); linspace(0,2,20)]'; wolffd@0: T = som_bmus(sM,Dtraj); wolffd@0: wolffd@0: som_show(sM,'comp',[1 1]); wolffd@0: som_show_add('traj',T,'Markercolor','r','TrajColor','r','subplot',1); wolffd@0: som_show_add('comet',T,'MarkerColor','r','subplot',2); wolffd@0: wolffd@0: % There's also a function SOM_TRAJECTORY which lauches a GUI wolffd@0: % specifically designed for displaying trajectories (in 'comet' wolffd@0: % mode). wolffd@0: wolffd@0: pause % Strike any key to learn about color handling... wolffd@0: wolffd@0: wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: clf wolffd@0: % COLOR HANDLING wolffd@0: % ============== wolffd@0: wolffd@0: % Matlab offers flexibility in the colormaps. Using the COLORMAP wolffd@0: % function, the colormap may be changed. There are several useful wolffd@0: % colormaps readily available, for example 'hot' and 'jet'. The wolffd@0: % default number of colors in the colormaps is 64. However, it is wolffd@0: % often advantageous to use less colors in the colormap. This way wolffd@0: % the components planes visualization become easier to interpret. wolffd@0: wolffd@0: % Here the three component planes are visualized using the 'hot' wolffd@0: % colormap and only three colors. wolffd@0: wolffd@0: som_show(sM,'comp',[1 2 3]) wolffd@0: colormap(hot(3)); wolffd@0: som_recolorbar wolffd@0: wolffd@0: pause % Press any key to change the colorbar labels... wolffd@0: wolffd@0: % The function SOM_RECOLORBAR can be used to reconfigure wolffd@0: % the labels beside the colorbar. wolffd@0: wolffd@0: % Here the colorbar of the first subplot is labeled using labels wolffd@0: % 'small', 'medium' and 'big' at values 0, 1 and 2. For the wolffd@0: % colorbar of the second subplot, values are calculated for the wolffd@0: % borders between colors. wolffd@0: wolffd@0: som_recolorbar(1,{[0 4 9]},'',{{'small','medium','big'}}); wolffd@0: som_recolorbar(2,'border',''); wolffd@0: wolffd@0: pause % Press any key to learn about SOM_NORMCOLOR... wolffd@0: wolffd@0: % Some SOM Toolbox functions do not use indexed colors if the wolffd@0: % underlying Matlab function (e.g. PLOT) do not use indexed wolffd@0: % colors. SOM_NORMCOLOR is a convenient function to simulate wolffd@0: % indexed colors: it calculates fixed RGB colors that wolffd@0: % are similar to indexed colors with the specified colormap. wolffd@0: wolffd@0: % Here, two SOM_GRID visualizations are created. One uses the wolffd@0: % 'surf' mode to show the component colors in indexed color wolffd@0: % mode, and the other uses SOM_NORMALIZE to do the same. wolffd@0: wolffd@0: clf wolffd@0: colormap(jet(64)) wolffd@0: subplot(1,2,1) wolffd@0: som_grid(sM,'Surf',sM.codebook(:,3)); wolffd@0: title('Surf mode') wolffd@0: wolffd@0: subplot(1,2,2) wolffd@0: som_grid(sM,'Markercolor',som_normcolor(sM.codebook(:,3))); wolffd@0: title('som\_normcolor') wolffd@0: wolffd@0: pause % Press any key to visualize different map shapes... wolffd@0: wolffd@0: wolffd@0: wolffd@0: clc wolffd@0: clf wolffd@0: % DIFFERENT MAP SHAPES wolffd@0: % ==================== wolffd@0: wolffd@0: % There's no direct way to visualize cylinder or toroid maps. When wolffd@0: % visualized, they are treated exactly as if they were sheet wolffd@0: % shaped. However, if function SOM_UNIT_COORDS is used to provide wolffd@0: % unit coordinates, then SOM_GRID can be used to visualize these wolffd@0: % alternative map shapes. wolffd@0: wolffd@0: % Here the grids of the three possible map shapes (sheet, cylinder wolffd@0: % and toroid) are visualized. The last subplot shows a component wolffd@0: % plane visualization of the toroid map. wolffd@0: wolffd@0: Cor = som_unit_coords(sM.topol.msize,'hexa','sheet'); wolffd@0: Coc = som_unit_coords(sM.topol.msize,'hexa','cyl'); wolffd@0: Cot = som_unit_coords(sM.topol.msize,'hexa','toroid'); wolffd@0: wolffd@0: subplot(2,2,1) wolffd@0: som_grid(sM,'Coord',Cor,'Markersize',3,'Linecolor','k'); wolffd@0: title('sheet'), view(0,-90), axis tight, axis equal wolffd@0: wolffd@0: subplot(2,2,2) wolffd@0: som_grid(sM,'Coord',Coc,'Markersize',3,'Linecolor','k'); wolffd@0: title('cylinder'), view(5,1), axis tight, axis equal wolffd@0: wolffd@0: subplot(2,2,3) wolffd@0: som_grid(sM,'Coord',Cot,'Markersize',3,'Linecolor','k'); wolffd@0: title('toroid'), view(-100,0), axis tight, axis equal wolffd@0: wolffd@0: subplot(2,2,4) wolffd@0: som_grid(sM,'Coord',Cot,'Surf',sM.codebook(:,3)); wolffd@0: colormap(jet), colorbar wolffd@0: title('toroid'), view(-100,0), axis tight, axis equal wolffd@0: wolffd@0: echo off