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