annotate toolboxes/FullBNT-1.0.7/bnt/examples/static/HME/hme_class_plot.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function fh=hme_class_plot(net, nodes_info, train_data, test_data)
wolffd@0 2 %
wolffd@0 3 % Use this function ONLY when the input dimension is 2
wolffd@0 4 % and the problem is a classification one.
wolffd@0 5 % We assume that each row of 'train_data' & 'test_data' is an example.
wolffd@0 6 %
wolffd@0 7 %------Line Spec------------------------------------------------------------------------
wolffd@0 8 %
wolffd@0 9 % LineWidth - specifies the width (in points) of the line
wolffd@0 10 % MarkerEdgeColor - specifies the color of the marker or the edge color
wolffd@0 11 % forfilled markers (circle, square, diamond, pentagram, hexagram, and the
wolffd@0 12 % four triangles).
wolffd@0 13 % MarkerFaceColor - specifies the color of the face of filled markers.
wolffd@0 14 % MarkerSize - specifies the size of the marker in points.
wolffd@0 15 %
wolffd@0 16 % Example
wolffd@0 17 % -------
wolffd@0 18 % plot(t,sin(2*t),'-mo',...
wolffd@0 19 % 'LineWidth',2,...
wolffd@0 20 % 'MarkerEdgeColor','k',... % 'k'=black
wolffd@0 21 % 'MarkerFaceColor',[.49 1 .63],... % RGB color
wolffd@0 22 % 'MarkerSize',12)
wolffd@0 23 %----------------------------------------------------------------------------------------
wolffd@0 24
wolffd@0 25 class_num=nodes_info(2,end);
wolffd@0 26 mn_x = round(min(train_data(:,1))); mx_x = round(max(train_data(:,1)));
wolffd@0 27 mn_y = round(min(train_data(:,2))); mx_y = round(max(train_data(:,2)));
wolffd@0 28 if nargin==4,
wolffd@0 29 mn_x = round(min([train_data(:,1); test_data(:,1)]));
wolffd@0 30 mx_x = round(max([train_data(:,1); test_data(:,1)]));
wolffd@0 31 mn_y = round(min([train_data(:,2); test_data(:,2)]));
wolffd@0 32 mx_y = round(max([train_data(:,1); test_data(:,2)]));
wolffd@0 33 end
wolffd@0 34 x = mn_x(1)-1:0.2:mx_x(1)+1;
wolffd@0 35 y = mn_y(1)-1:0.2:mx_y(1)+1;
wolffd@0 36 [X, Y] = meshgrid(x,y);
wolffd@0 37 X = X(:);
wolffd@0 38 Y = Y(:);
wolffd@0 39 num_g=size(X,1);
wolffd@0 40 griglia = [X Y];
wolffd@0 41 rand('state',1);
wolffd@0 42 if class_num<=6,
wolffd@0 43 colors=['r'; 'g'; 'b'; 'c'; 'm'; 'y'];
wolffd@0 44 else
wolffd@0 45 colors=rand(class_num, 3); % each row is an RGB color
wolffd@0 46 end
wolffd@0 47 fh = figure('Name','Data & decision boundaries', 'MenuBar', 'none', 'NumberTitle', 'off');
wolffd@0 48 ms=5; % Marker Size
wolffd@0 49 if nargin==4,
wolffd@0 50 % ms=4; % Marker Size
wolffd@0 51 subplot(1,2,1);
wolffd@0 52 end
wolffd@0 53 % Plot of train_set -------------------------------------------------------------------------
wolffd@0 54 axis([mn_x-1 mx_x+1 mn_y-1 mx_y+1]);
wolffd@0 55 set(gca, 'Box', 'on');
wolffd@0 56 c_max_train = max(train_data(:,3));
wolffd@0 57 hold on
wolffd@0 58 for m=1:c_max_train,
wolffd@0 59 app_x=train_data(:,1);
wolffd@0 60 app_y=train_data(:,2);
wolffd@0 61 thisX=app_x(train_data(:,3)==m);
wolffd@0 62 thisY=app_y(train_data(:,3)==m);
wolffd@0 63 if class_num<=6,
wolffd@0 64 str_col=[];
wolffd@0 65 str_col=['o', colors(m,:)];
wolffd@0 66 plot(thisX, thisY, str_col, 'MarkerSize', ms);
wolffd@0 67 else
wolffd@0 68 plot(thisX, thisY, 'o',...
wolffd@0 69 'LineWidth', 1,...
wolffd@0 70 'MarkerEdgeColor', colors(m,:), 'MarkerSize', ms)
wolffd@0 71 end
wolffd@0 72 end
wolffd@0 73 %---hmefwd_generale(net,data,ndata)-----------------------------------------------------------
wolffd@0 74 Z=fhme(net, nodes_info, griglia, num_g); % forward propagation trougth the HME
wolffd@0 75 %---------------------------------------------------------------------------------------------
wolffd@0 76 [foo , class] = max(Z'); % 0/1 loss function => we assume that the true class is the one with the
wolffd@0 77 % maximum posterior prob.
wolffd@0 78 class = class';
wolffd@0 79 for m = 1:class_num,
wolffd@0 80 thisX=[]; thisY=[];
wolffd@0 81 thisX = X(class == m);
wolffd@0 82 thisY = Y(class == m);
wolffd@0 83 if class_num<=6,
wolffd@0 84 str_col=[];
wolffd@0 85 str_col=['d', colors(m,:)];
wolffd@0 86 h=plot(thisX, thisY, str_col);
wolffd@0 87 else
wolffd@0 88 h = plot(thisX, thisY, 'd',...
wolffd@0 89 'MarkerEdgeColor',colors(m,:),...
wolffd@0 90 'MarkerFaceColor','w');
wolffd@0 91 end
wolffd@0 92 set(h, 'MarkerSize', 4);
wolffd@0 93 end
wolffd@0 94 title('Training set and Decision Boundaries (0/1 loss)')
wolffd@0 95 hold off
wolffd@0 96
wolffd@0 97 % Plot of test_set --------------------------------------------------------------------------
wolffd@0 98 if nargin==4,
wolffd@0 99 subplot(1,2,2);
wolffd@0 100 axis([mn_x-1 mx_x+1 mn_y-1 mx_y+1]);
wolffd@0 101 set(gca, 'Box', 'on');
wolffd@0 102 hold on
wolffd@0 103 if size(test_data,2)==3, % we know the classification of the test set examples
wolffd@0 104 c_max_test = max(test_data(:,3));
wolffd@0 105 for m=1:c_max_test,
wolffd@0 106 app_x=test_data(:,1);
wolffd@0 107 app_y=test_data(:,2);
wolffd@0 108 thisX=app_x(test_data(:,3)==m);
wolffd@0 109 thisY=app_y(test_data(:,3)==m);
wolffd@0 110 if class_num<=6,
wolffd@0 111 str_col=[];
wolffd@0 112 str_col=['o', colors(m,:)];
wolffd@0 113 plot(thisX, thisY, str_col, 'MarkerSize', ms);
wolffd@0 114 else
wolffd@0 115 plot(thisX, thisY, 'o',...
wolffd@0 116 'LineWidth', 1,...
wolffd@0 117 'MarkerEdgeColor', colors(m,:),...
wolffd@0 118 'MarkerSize',ms);
wolffd@0 119 end
wolffd@0 120 end
wolffd@0 121 else
wolffd@0 122 plot(test_data(:,1), test_data(:,2), 'ko',...
wolffd@0 123 'MarkerSize', ms);
wolffd@0 124 end
wolffd@0 125 for m = 1:class_num,
wolffd@0 126 thisX=[]; thisY=[];
wolffd@0 127 thisX = X(class == m);
wolffd@0 128 thisY = Y(class == m);
wolffd@0 129 if class_num<=6,
wolffd@0 130 str_col=[];
wolffd@0 131 str_col=['d', colors(m,:)];
wolffd@0 132 h=plot(thisX, thisY, str_col);
wolffd@0 133 else
wolffd@0 134 h = plot(thisX, thisY, 'd',...
wolffd@0 135 'MarkerEdgeColor', colors(m,:),...
wolffd@0 136 'MarkerFaceColor','w');
wolffd@0 137 end
wolffd@0 138 set(h, 'MarkerSize', 4);
wolffd@0 139 end
wolffd@0 140 title('Test set and Decision Boundaries (0/1 loss)')
wolffd@0 141 hold off
wolffd@0 142 end