📄 som_demo3.m
字号:
pause % Strike any key to continue...% Besides SOM_SHOW and SOM_CPLANE, there are three other% functions specifically designed for showing the values of the % component planes: SOM_PIEPLANE, SOM_BARPLANE, SOM_PLOTPLANE. % SOM_PIEPLANE shows a single pie chart for each map unit. Each% pie shows the relative proportion of each component of the sum of% all components in that map unit. The component values must be% positive. % SOM_BARPLANE shows a barchart in each map unit. The scaling of % bars can be made unit-wise or variable-wise. By default it is% determined variable-wise.% SOM_PLOTPLANE shows a linegraph in each map unit. M = som_normalize(sM.codebook,'range'); subplot(1,3,1)som_pieplane(sM, M);title('som\_pieplane')subplot(1,3,2)som_barplane(sM, M, '', 'unitwise');title('som\_barplane')subplot(1,3,3)som_plotplane(sM, M, 'b');title('som\_plotplane')pause % Strike any key to visualize cluster properties...clfclc% 2. VISUALIZATION OF COMPONENTS: CLUSTERS% ========================================% An interesting question is of course how do the values of the% variables relate to the clusters: what are the values of the% components in the clusters, and which components are the ones% which *make* the clusters.som_show(sM)% From the U-matrix and component planes, one can easily see% what the typical values are in each cluster. pause % Strike any key to continue...% The significance of the components with respect to the clustering% is harder to visualize. One indication of importance is that on% the borders of the clusters, values of important variables change% very rabidly.% Here, the distance matrix is calculated with respect to each% variable. u1 = som_umat(sM,'mask',[1 0 0]'); u1=u1(1:2:size(u1,1),1:2:size(u1,2));u2 = som_umat(sM,'mask',[0 1 0]'); u2=u2(1:2:size(u2,1),1:2:size(u2,2));u3 = som_umat(sM,'mask',[0 0 1]'); u3=u3(1:2:size(u3,1),1:2:size(u3,2));% Here, the distance matrices are shown, as well as a piechart% indicating the relative importance of each variable in each% map unit. The size of piecharts has been scaled by the% distance matrix calculated from all components.subplot(2,2,1)som_cplane(sM,u1(:));title(sM.comp_names{1})subplot(2,2,2)som_cplane(sM,u2(:));title(sM.comp_names{2})subplot(2,2,3)som_cplane(sM,u3(:));title(sM.comp_names{3})subplot(2,2,4)som_pieplane(sM, [u1(:), u2(:), u3(:)], hsv(3), Um(:)/max(Um(:)));title('Relative importance')% From the last subplot, one can see that in the area where the% bigger cluster border is, the 'X-coord' component (red color)% has biggest effect, and thus is the main factor in separating% that cluster from the rest.pause % Strike any key to learn about correlation hunting...clfclc% 2. VISUALIZATION OF COMPONENTS: CORRELATION HUNTING% ===================================================% Finally, the component planes are often used for correlation% hunting. When the number of variables is high, the component% plane visualization offers a convenient way to visualize all% components at once and hunt for correlations (as opposed to% N*(N-1)/2 scatterplots).% Hunting correlations this way is not very accurate. However, it% is easy to select interesting combinations for further% investigation.% Here, the first and third components are shown with scatter% plot. As with projections, a color coding is used to link the% visualization to the map plane. In the color coding, size shows% the distance matrix information.C = som_colorcode(sM);subplot(1,2,1)som_cplane(sM,C,1-Um(:)/max(Um(:)));title('Color coding + distance matrix')subplot(1,2,2)som_grid(sM,'Coord',sM.codebook(:,[1 3]),'MarkerColor',C);title('Scatter plot'); xlabel(sM.comp_names{1}); ylabel(sM.comp_names{3})axis equalpause % Strike any key to visualize data responses...clfclc% 3. DATA ON MAP% ==============% The SOM is a map of the data manifold. An interesting question% then is where on the map a specific data sample is located, and% how accurate is that localization? One is interested in the% response of the map to the data sample. % The simplest answer is to find the BMU of the data sample.% However, this gives no indication of the accuracy of the% match. Is the data sample close to the BMU, or is it actually% equally close to the neighboring map units (or even approximately% as close to all map units)? Sometimes accuracy doesn't really% matter, but if it does, it should be visualized somehow.% Here are different kinds of response visualizations for two% vectors: [0 0 0] and [99 99 99]. % - BMUs indicated with labels % - BMUs indicated with markers, relative quantization errors% (in this case, proportion between distances to BMU and % Worst-MU) with vertical lines% - quantization error between the samples and all map units % - fuzzy response (a non-linear function of quantization% error) of all map unitsecho off[bm,qe] = som_bmus(sM,[0 0 0; 99 99 99],'all'); % distance to all map units[dummy,ind] = sort(bm(1,:)); d0 = qe(1,ind)'; [dummy,ind] = sort(bm(2,:)); d9 = qe(2,ind)'; bmu0 = bm(1,1); bmu9 = bm(2,1); % bmush0 = zeros(prod(sM.topol.msize),1); h0(bmu0) = 1; % crisp hitsh9 = zeros(prod(sM.topol.msize),1); h9(bmu9) = 1; lab = cell(prod(sM.topol.msize),1); lab{bmu0} = '[0,0,0]'; lab{bmu9} = '[99,99,99]';hf0 = som_hits(sM,[0 0 0],'fuzzy'); % fuzzy responsehf9 = som_hits(sM,[99 99 99],'fuzzy'); som_show(sM,'umat',{'all','BMU'},... 'color',{d0,'Qerror 0'},'color',{hf0,'Fuzzy response 0'},... 'empty','BMU+qerror',... 'color',{d9,'Qerror 99'},'color',{hf9,'Fuzzy response 99'}); som_show_add('label',lab,'Subplot',1,'Textcolor','r');som_show_add('hit',[h0, h9],'Subplot',4,'MarkerColor','r');hold onCo = som_vis_coords(sM.topol.lattice,sM.topol.msize);plot3(Co(bmu0,[1 1]),Co(bmu0,[2 2]),[0 10*qe(1,1)/qe(1,end)],'r-')plot3(Co(bmu9,[1 1]),Co(bmu9,[2 2]),[0 10*qe(2,1)/qe(2,end)],'r-')view(3), axis equalecho on% Here are the distances to BMU, 2-BMU and WMU:qe(1,[1,2,end]) % [0 0 0]qe(2,[1,2,end]) % [99 99 99]% One can see that for [0 0 0] the accuracy is pretty good as the% quantization error of the BMU is much lower than that of the% WMU. On the other hand [99 99 99] is very far from the map:% distance to BMU is almost equal to distance to WMU.pause % Strike any key to visualize responses of multiple samples...clcclf% 3. DATA ON MAP: HIT HISTOGRAMS% ==============================% One can also investigate whole data sets using the map. When the% BMUs of multiple data samples are aggregated, a hit histogram% results. Instead of BMUs, one can also aggregate for example% fuzzy responses.% The hit histograms (or aggregated responses) can then be compared% with each other. % Here are hit histograms of three data sets: one with 50 first% vectors of the data set, one with 150 samples from the data% set, and one with 50 randomly selected samples. In the last% subplot, the fuzzy response of the first data set.dlen = size(sD.data,1);Dsample1 = sD.data(1:50,:); h1 = som_hits(sM,Dsample1); Dsample2 = sD.data(1:150,:); h2 = som_hits(sM,Dsample2); Dsample3 = sD.data(ceil(rand(50,1)*dlen),:); h3 = som_hits(sM,Dsample3); hf = som_hits(sM,Dsample1,'fuzzy');som_show(sM,'umat','all','umat','all','umat','all','color',{hf,'Fuzzy'})som_show_add('hit',h1,'Subplot',1,'Markercolor','r')som_show_add('hit',h2,'Subplot',2,'Markercolor','r')som_show_add('hit',h3,'Subplot',3,'Markercolor','r')pause % Strike any key to visualize trajectories...clcclf% 3. DATA ON MAP: TRAJECTORIES% ============================% A special data mapping technique is trajectory. If the samples% are ordered, forming a time-series for example, their response on% the map can be tracked. The function SOM_SHOW_ADD can be used to% show the trajectories in two different modes: 'traj' and 'comet'.% Here, a series of data points is formed which go from [8,0,0]% to [2,2,2]. The trajectory is plotted using the two modes.Dtraj = [linspace(9,2,20); linspace(0,2,20); linspace(0,2,20)]';T = som_bmus(sM,Dtraj);som_show(sM,'comp',[1 1]);som_show_add('traj',T,'Markercolor','r','TrajColor','r','subplot',1);som_show_add('comet',T,'MarkerColor','r','subplot',2);% There's also a function SOM_TRAJECTORY which lauches a GUI% specifically designed for displaying trajectories (in 'comet'% mode).pause % Strike any key to learn about color handling...clcclf% COLOR HANDLING% ==============% Matlab offers flexibility in the colormaps. Using the COLORMAP% function, the colormap may be changed. There are several useful% colormaps readily available, for example 'hot' and 'jet'. The% default number of colors in the colormaps is 64. However, it is% often advantageous to use less colors in the colormap. This way% the components planes visualization become easier to interpret.% Here the three component planes are visualized using the 'hot'% colormap and only three colors.som_show(sM,'comp',[1 2 3])colormap(hot(3));som_recolorbar pause % Press any key to change the colorbar labels...% The function SOM_RECOLORBAR can be used to reconfigure% the labels beside the colorbar. % Here the colorbar of the first subplot is labeled using labels% 'small', 'medium' and 'big' at values 0, 1 and 2. For the% colorbar of the second subplot, values are calculated for the% borders between colors.som_recolorbar(1,{[0 4 9]},'',{{'small','medium','big'}});som_recolorbar(2,'border','');pause % Press any key to learn about SOM_NORMCOLOR...% Some SOM Toolbox functions do not use indexed colors if the% underlying Matlab function (e.g. PLOT) do not use indexed% colors. SOM_NORMCOLOR is a convenient function to simulate% indexed colors: it calculates fixed RGB colors that% are similar to indexed colors with the specified colormap. % Here, two SOM_GRID visualizations are created. One uses the% 'surf' mode to show the component colors in indexed color% mode, and the other uses SOM_NORMALIZE to do the same. clfcolormap(jet(64))subplot(1,2,1)som_grid(sM,'Surf',sM.codebook(:,3));title('Surf mode')subplot(1,2,2)som_grid(sM,'Markercolor',som_normcolor(sM.codebook(:,3)));title('som\_normcolor')pause % Press any key to visualize different map shapes...clcclf% DIFFERENT MAP SHAPES% ====================% There's no direct way to visualize cylinder or toroid maps. When% visualized, they are treated exactly as if they were sheet% shaped. However, if function SOM_UNIT_COORDS is used to provide% unit coordinates, then SOM_GRID can be used to visualize these% alternative map shapes.% Here the grids of the three possible map shapes (sheet, cylinder% and toroid) are visualized. The last subplot shows a component % plane visualization of the toroid map.Cor = som_unit_coords(sM.topol.msize,'hexa','sheet');Coc = som_unit_coords(sM.topol.msize,'hexa','cyl');Cot = som_unit_coords(sM.topol.msize,'hexa','toroid');subplot(2,2,1)som_grid(sM,'Coord',Cor,'Markersize',3,'Linecolor','k');title('sheet'), view(0,-90), axis tight, axis equalsubplot(2,2,2)som_grid(sM,'Coord',Coc,'Markersize',3,'Linecolor','k');title('cylinder'), view(5,1), axis tight, axis equalsubplot(2,2,3)som_grid(sM,'Coord',Cot,'Markersize',3,'Linecolor','k');title('toroid'), view(-100,0), axis tight, axis equalsubplot(2,2,4)som_grid(sM,'Coord',Cot,'Surf',sM.codebook(:,3));colormap(jet), colorbartitle('toroid'), view(-100,0), axis tight, axis equalecho off
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -