annotate toolboxes/FullBNT-1.0.7/netlab3.3/demglm2.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 %DEMGLM2 Demonstrate simple classification using a generalized linear model.
wolffd@0 2 %
wolffd@0 3 % Description
wolffd@0 4 % The problem consists of a two dimensional input matrix DATA and a
wolffd@0 5 % vector of classifications T. The data is generated from three
wolffd@0 6 % Gaussian clusters, and a generalized linear model with softmax output
wolffd@0 7 % is trained using iterative reweighted least squares. A plot of the
wolffd@0 8 % data together with regions shaded by the classification given by the
wolffd@0 9 % network is generated.
wolffd@0 10 %
wolffd@0 11 % See also
wolffd@0 12 % DEMGLM1, GLM, GLMTRAIN
wolffd@0 13 %
wolffd@0 14
wolffd@0 15 % Copyright (c) Ian T Nabney (1996-2001)
wolffd@0 16
wolffd@0 17
wolffd@0 18 % Generate data from three classes in 2d
wolffd@0 19 input_dim = 2;
wolffd@0 20
wolffd@0 21 % Fix seeds for reproducible results
wolffd@0 22 randn('state', 42);
wolffd@0 23 rand('state', 42);
wolffd@0 24
wolffd@0 25 ndata = 100;
wolffd@0 26 % Generate mixture of three Gaussians in two dimensional space
wolffd@0 27 mix = gmm(2, 3, 'spherical');
wolffd@0 28 mix.priors = [0.4 0.3 0.3]; % Cluster priors
wolffd@0 29 mix.centres = [2, 2; 0.0, 0.0; 1, -1]; % Cluster centres
wolffd@0 30 mix.covars = [0.5 1.0 0.6];
wolffd@0 31
wolffd@0 32 [data, label] = gmmsamp(mix, ndata);
wolffd@0 33 id = eye(3);
wolffd@0 34 targets = id(label,:);
wolffd@0 35
wolffd@0 36 % Plot the result
wolffd@0 37
wolffd@0 38 clc
wolffd@0 39 disp('This demonstration illustrates the use of a generalized linear model')
wolffd@0 40 disp('to classify data from three classes in a two-dimensional space. We')
wolffd@0 41 disp('begin by generating and plotting the data.')
wolffd@0 42 disp(' ')
wolffd@0 43 disp('Press any key to continue.')
wolffd@0 44 pause
wolffd@0 45
wolffd@0 46 fh1 = figure;
wolffd@0 47 plot(data(label==1,1), data(label==1,2), 'bo');
wolffd@0 48 hold on
wolffd@0 49 axis([-4 5 -4 5]);
wolffd@0 50 set(gca, 'Box', 'on')
wolffd@0 51 plot(data(label==2,1), data(label==2,2), 'rx')
wolffd@0 52 plot(data(label==3, 1), data(label==3, 2), 'go')
wolffd@0 53 title('Data')
wolffd@0 54
wolffd@0 55 clc
wolffd@0 56 disp('Now we fit a model consisting of a softmax function of')
wolffd@0 57 disp('a linear combination of the input variables.')
wolffd@0 58 disp(' ')
wolffd@0 59 disp('The model is trained using the IRLS algorithm for up to 10 iterations')
wolffd@0 60 disp(' ')
wolffd@0 61 disp('Press any key to continue.')
wolffd@0 62 pause
wolffd@0 63
wolffd@0 64 net = glm(input_dim, size(targets, 2), 'softmax');
wolffd@0 65 options = foptions;
wolffd@0 66 options(1) = 1;
wolffd@0 67 options(14) = 10;
wolffd@0 68 net = glmtrain(net, options, data, targets);
wolffd@0 69
wolffd@0 70 disp(' ')
wolffd@0 71 disp('We now plot the decision regions given by this model.')
wolffd@0 72 disp(' ')
wolffd@0 73 disp('Press any key to continue.')
wolffd@0 74 pause
wolffd@0 75
wolffd@0 76 x = -4.0:0.2:5.0;
wolffd@0 77 y = -4.0:0.2:5.0;
wolffd@0 78 [X, Y] = meshgrid(x,y);
wolffd@0 79 X = X(:);
wolffd@0 80 Y = Y(:);
wolffd@0 81 grid = [X Y];
wolffd@0 82 Z = glmfwd(net, grid);
wolffd@0 83 [foo , class] = max(Z');
wolffd@0 84 class = class';
wolffd@0 85 colors = ['b.'; 'r.'; 'g.'];
wolffd@0 86 for i = 1:3
wolffd@0 87 thisX = X(class == i);
wolffd@0 88 thisY = Y(class == i);
wolffd@0 89 h = plot(thisX, thisY, colors(i,:));
wolffd@0 90 set(h, 'MarkerSize', 8);
wolffd@0 91 end
wolffd@0 92 title('Plot of Decision regions')
wolffd@0 93
wolffd@0 94 hold off
wolffd@0 95
wolffd@0 96 clc
wolffd@0 97 disp('Note that the boundaries of decision regions are straight lines.')
wolffd@0 98 disp(' ')
wolffd@0 99 disp('Press any key to end.')
wolffd@0 100 pause
wolffd@0 101 close(fh1);
wolffd@0 102 clear all;
wolffd@0 103