wolffd@0: %DEMGLM2 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 three wolffd@0: % Gaussian clusters, and a generalized linear model with softmax output wolffd@0: % is trained using iterative reweighted least squares. A plot of the wolffd@0: % data together with regions shaded by the classification given by the wolffd@0: % network is generated. wolffd@0: % wolffd@0: % See also wolffd@0: % DEMGLM1, GLM, GLMTRAIN wolffd@0: % wolffd@0: wolffd@0: % Copyright (c) Ian T Nabney (1996-2001) wolffd@0: wolffd@0: wolffd@0: % Generate data from three 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 three Gaussians in two dimensional space wolffd@0: mix = gmm(2, 3, 'spherical'); wolffd@0: mix.priors = [0.4 0.3 0.3]; % Cluster priors wolffd@0: mix.centres = [2, 2; 0.0, 0.0; 1, -1]; % Cluster centres wolffd@0: mix.covars = [0.5 1.0 0.6]; wolffd@0: wolffd@0: [data, label] = gmmsamp(mix, ndata); wolffd@0: id = eye(3); wolffd@0: targets = id(label,:); 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 three 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: plot(data(label==3, 1), data(label==3, 2), 'go') wolffd@0: title('Data') wolffd@0: wolffd@0: clc wolffd@0: disp('Now we fit a model consisting of a softmax 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 up to 10 iterations') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: wolffd@0: net = glm(input_dim, size(targets, 2), 'softmax'); wolffd@0: options = foptions; wolffd@0: options(1) = 1; wolffd@0: options(14) = 10; wolffd@0: net = glmtrain(net, options, data, targets); wolffd@0: wolffd@0: disp(' ') wolffd@0: disp('We now plot the decision regions given by this model.') wolffd@0: disp(' ') wolffd@0: disp('Press any key to continue.') wolffd@0: pause wolffd@0: 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: [foo , class] = max(Z'); wolffd@0: class = class'; wolffd@0: colors = ['b.'; 'r.'; 'g.']; wolffd@0: for i = 1:3 wolffd@0: thisX = X(class == i); wolffd@0: thisY = Y(class == i); wolffd@0: h = plot(thisX, thisY, colors(i,:)); wolffd@0: set(h, 'MarkerSize', 8); wolffd@0: end wolffd@0: title('Plot of Decision regions') wolffd@0: wolffd@0: hold off wolffd@0: wolffd@0: clc wolffd@0: disp('Note that the boundaries of decision regions 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: