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