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