wolffd@0: %DEMGLM1 Demonstrate simple classification using a generalized linear model. wolffd@0: % wolffd@0: % Description wolffd@0: % The problem consists of a two dimensional input matrix DATA and a wolffd@0: % vector of classifications T. The data is generated from two wolffd@0: % Gaussian clusters, and a generalized linear model with logistic wolffd@0: % output is trained using iterative reweighted least squares. A plot of wolffd@0: % the data together with the 0.1, 0.5 and 0.9 contour lines of the wolffd@0: % conditional probability is generated. wolffd@0: % wolffd@0: % See also wolffd@0: % DEMGLM2, GLM, GLMTRAIN wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: wolffd@0: % Generate data from two classes in 2d wolffd@0: input_dim = 2; wolffd@0: wolffd@0: % Fix seeds for reproducible results wolffd@0: randn('state', 42); wolffd@0: rand('state', 42); wolffd@0: wolffd@0: ndata = 100; wolffd@0: % Generate mixture of two Gaussians in two dimensional space wolffd@0: mix = gmm(2, 2, 'spherical'); wolffd@0: mix.priors = [0.4 0.6]; % Cluster priors wolffd@0: mix.centres = [2.0, 2.0; 0.0, 0.0]; % Cluster centres wolffd@0: mix.covars = [0.5, 1.0]; wolffd@0: wolffd@0: [data, label] = gmmsamp(mix, ndata); wolffd@0: targets = label - ones(ndata, 1); wolffd@0: wolffd@0: % Plot the result wolffd@0: wolffd@0: clc wolffd@0: disp('This demonstration illustrates the use of a generalized linear model') wolffd@0: disp('to classify data from two classes in a two-dimensional space. We') wolffd@0: disp('begin by generating and plotting the data.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: wolffd@0: fh1 = figure; wolffd@0: plot(data(label==1,1), data(label==1,2), 'bo'); wolffd@0: hold on wolffd@0: axis([-4 5 -4 5]) wolffd@0: set(gca, 'box', 'on') wolffd@0: plot(data(label==2,1), data(label==2,2), 'rx') wolffd@0: title('Data') wolffd@0: wolffd@0: clc wolffd@0: disp('Now we fit a model consisting of a logistic sigmoid function of') wolffd@0: disp('a linear combination of the input variables.') wolffd@0: disp(' ') wolffd@0: disp('The model is trained using the IRLS algorithm for 5 iterations') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: wolffd@0: net = glm(input_dim, 1, 'logistic'); wolffd@0: options = foptions; wolffd@0: options(1) = 1; wolffd@0: options(14) = 5; wolffd@0: net = glmtrain(net, options, data, targets); wolffd@0: wolffd@0: disp(' ') wolffd@0: disp('We now plot some density contours given by this model.') wolffd@0: disp('The contour labelled 0.5 is the decision boundary.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: x = -4.0:0.2:5.0; wolffd@0: y = -4.0:0.2:5.0; wolffd@0: [X, Y] = meshgrid(x,y); wolffd@0: X = X(:); wolffd@0: Y = Y(:); wolffd@0: grid = [X Y]; wolffd@0: Z = glmfwd(net, grid); wolffd@0: Z = reshape(Z, length(x), length(y)); wolffd@0: v = [0.1 0.5 0.9]; wolffd@0: [c, h] = contour(x, y, Z, v); wolffd@0: title('Generalized Linear Model') wolffd@0: set(h, 'linewidth', 3) wolffd@0: clabel(c, h); wolffd@0: wolffd@0: clc wolffd@0: disp('Note that the contours of constant density are straight lines.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to end.') wolffd@0: pause wolffd@0: close(fh1); wolffd@0: clear all; wolffd@0: