Daniel@0: %DEMGLM2 Demonstrate simple classification using a generalized linear model. Daniel@0: % Daniel@0: % Description Daniel@0: % The problem consists of a two dimensional input matrix DATA and a Daniel@0: % vector of classifications T. The data is generated from three Daniel@0: % Gaussian clusters, and a generalized linear model with softmax output Daniel@0: % is trained using iterative reweighted least squares. A plot of the Daniel@0: % data together with regions shaded by the classification given by the Daniel@0: % network is generated. Daniel@0: % Daniel@0: % See also Daniel@0: % DEMGLM1, GLM, GLMTRAIN Daniel@0: % Daniel@0: Daniel@0: % Copyright (c) Ian T Nabney (1996-2001) Daniel@0: Daniel@0: Daniel@0: % Generate data from three classes in 2d Daniel@0: input_dim = 2; Daniel@0: Daniel@0: % Fix seeds for reproducible results Daniel@0: randn('state', 42); Daniel@0: rand('state', 42); Daniel@0: Daniel@0: ndata = 100; Daniel@0: % Generate mixture of three Gaussians in two dimensional space Daniel@0: mix = gmm(2, 3, 'spherical'); Daniel@0: mix.priors = [0.4 0.3 0.3]; % Cluster priors Daniel@0: mix.centres = [2, 2; 0.0, 0.0; 1, -1]; % Cluster centres Daniel@0: mix.covars = [0.5 1.0 0.6]; Daniel@0: Daniel@0: [data, label] = gmmsamp(mix, ndata); Daniel@0: id = eye(3); Daniel@0: targets = id(label,:); Daniel@0: Daniel@0: % Plot the result Daniel@0: Daniel@0: clc Daniel@0: disp('This demonstration illustrates the use of a generalized linear model') Daniel@0: disp('to classify data from three classes in a two-dimensional space. We') Daniel@0: disp('begin by generating and plotting the data.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: Daniel@0: fh1 = figure; Daniel@0: plot(data(label==1,1), data(label==1,2), 'bo'); Daniel@0: hold on Daniel@0: axis([-4 5 -4 5]); Daniel@0: set(gca, 'Box', 'on') Daniel@0: plot(data(label==2,1), data(label==2,2), 'rx') Daniel@0: plot(data(label==3, 1), data(label==3, 2), 'go') Daniel@0: title('Data') Daniel@0: Daniel@0: clc Daniel@0: disp('Now we fit a model consisting of a softmax function of') Daniel@0: disp('a linear combination of the input variables.') Daniel@0: disp(' ') Daniel@0: disp('The model is trained using the IRLS algorithm for up to 10 iterations') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: Daniel@0: net = glm(input_dim, size(targets, 2), 'softmax'); Daniel@0: options = foptions; Daniel@0: options(1) = 1; Daniel@0: options(14) = 10; Daniel@0: net = glmtrain(net, options, data, targets); Daniel@0: Daniel@0: disp(' ') Daniel@0: disp('We now plot the decision regions given by this model.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to continue.') Daniel@0: pause Daniel@0: Daniel@0: x = -4.0:0.2:5.0; Daniel@0: y = -4.0:0.2:5.0; Daniel@0: [X, Y] = meshgrid(x,y); Daniel@0: X = X(:); Daniel@0: Y = Y(:); Daniel@0: grid = [X Y]; Daniel@0: Z = glmfwd(net, grid); Daniel@0: [foo , class] = max(Z'); Daniel@0: class = class'; Daniel@0: colors = ['b.'; 'r.'; 'g.']; Daniel@0: for i = 1:3 Daniel@0: thisX = X(class == i); Daniel@0: thisY = Y(class == i); Daniel@0: h = plot(thisX, thisY, colors(i,:)); Daniel@0: set(h, 'MarkerSize', 8); Daniel@0: end Daniel@0: title('Plot of Decision regions') Daniel@0: Daniel@0: hold off Daniel@0: Daniel@0: clc Daniel@0: disp('Note that the boundaries of decision regions are straight lines.') Daniel@0: disp(' ') Daniel@0: disp('Press any key to end.') Daniel@0: pause Daniel@0: close(fh1); Daniel@0: clear all; Daniel@0: