annotate toolboxes/FullBNT-1.0.7/netlab3.3/demglm1.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 %DEMGLM1 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 two
wolffd@0 6 % Gaussian clusters, and a generalized linear model with logistic
wolffd@0 7 % output is trained using iterative reweighted least squares. A plot of
wolffd@0 8 % the data together with the 0.1, 0.5 and 0.9 contour lines of the
wolffd@0 9 % conditional probability is generated.
wolffd@0 10 %
wolffd@0 11 % See also
wolffd@0 12 % DEMGLM2, 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 two 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 two Gaussians in two dimensional space
wolffd@0 27 mix = gmm(2, 2, 'spherical');
wolffd@0 28 mix.priors = [0.4 0.6]; % Cluster priors
wolffd@0 29 mix.centres = [2.0, 2.0; 0.0, 0.0]; % Cluster centres
wolffd@0 30 mix.covars = [0.5, 1.0];
wolffd@0 31
wolffd@0 32 [data, label] = gmmsamp(mix, ndata);
wolffd@0 33 targets = label - ones(ndata, 1);
wolffd@0 34
wolffd@0 35 % Plot the result
wolffd@0 36
wolffd@0 37 clc
wolffd@0 38 disp('This demonstration illustrates the use of a generalized linear model')
wolffd@0 39 disp('to classify data from two classes in a two-dimensional space. We')
wolffd@0 40 disp('begin by generating and plotting the data.')
wolffd@0 41 disp(' ')
wolffd@0 42 disp('Press any key to continue.')
wolffd@0 43 pause
wolffd@0 44
wolffd@0 45 fh1 = figure;
wolffd@0 46 plot(data(label==1,1), data(label==1,2), 'bo');
wolffd@0 47 hold on
wolffd@0 48 axis([-4 5 -4 5])
wolffd@0 49 set(gca, 'box', 'on')
wolffd@0 50 plot(data(label==2,1), data(label==2,2), 'rx')
wolffd@0 51 title('Data')
wolffd@0 52
wolffd@0 53 clc
wolffd@0 54 disp('Now we fit a model consisting of a logistic sigmoid function of')
wolffd@0 55 disp('a linear combination of the input variables.')
wolffd@0 56 disp(' ')
wolffd@0 57 disp('The model is trained using the IRLS algorithm for 5 iterations')
wolffd@0 58 disp(' ')
wolffd@0 59 disp('Press any key to continue.')
wolffd@0 60 pause
wolffd@0 61
wolffd@0 62 net = glm(input_dim, 1, 'logistic');
wolffd@0 63 options = foptions;
wolffd@0 64 options(1) = 1;
wolffd@0 65 options(14) = 5;
wolffd@0 66 net = glmtrain(net, options, data, targets);
wolffd@0 67
wolffd@0 68 disp(' ')
wolffd@0 69 disp('We now plot some density contours given by this model.')
wolffd@0 70 disp('The contour labelled 0.5 is the decision boundary.')
wolffd@0 71 disp(' ')
wolffd@0 72 disp('Press any key to continue.')
wolffd@0 73 pause
wolffd@0 74 x = -4.0:0.2:5.0;
wolffd@0 75 y = -4.0:0.2:5.0;
wolffd@0 76 [X, Y] = meshgrid(x,y);
wolffd@0 77 X = X(:);
wolffd@0 78 Y = Y(:);
wolffd@0 79 grid = [X Y];
wolffd@0 80 Z = glmfwd(net, grid);
wolffd@0 81 Z = reshape(Z, length(x), length(y));
wolffd@0 82 v = [0.1 0.5 0.9];
wolffd@0 83 [c, h] = contour(x, y, Z, v);
wolffd@0 84 title('Generalized Linear Model')
wolffd@0 85 set(h, 'linewidth', 3)
wolffd@0 86 clabel(c, h);
wolffd@0 87
wolffd@0 88 clc
wolffd@0 89 disp('Note that the contours of constant density are straight lines.')
wolffd@0 90 disp(' ')
wolffd@0 91 disp('Press any key to end.')
wolffd@0 92 pause
wolffd@0 93 close(fh1);
wolffd@0 94 clear all;
wolffd@0 95