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

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