% exampleclassif.m April 26, 2006 R.F. Murphy % illustrate training and testing a classifier and displaying decision boundary % % first generate some training and test data % (this section could be replaced by reading some real data) train=rand(40,2); test=rand(10,2); % shift the second half of the training and test data for the second variable train(21:40,2)=train(21:40,2)+1; test(6:10,2)=test(6:10,2)+1; % plot the training data for the first half as blue circles and the % second half as green diamonds plot(train(1:20,1),train(1:20,2),'bo',train(21:40,1),train(21:40,2),'gd'); hold on % plot the test data for the first half as blue x's and the second half % as green plusses plot(test(1:5,1),test(1:5,2),'bx',test(6:10,1),test(6:10,2),'g+'); hold off % generate "labels" for each row showing that the first half is in class 1 group=[repmat(1,20,1); repmat(2,20,1)]; % % train a linear classifier with the training data and evaluate it with % the test data % (this line could be replaced with a call to any other type of classifier) cl=classify(test,train,group); % the resulting vector shows which class the classifier thinks each test % point is in cl % count true +, false +, true -, false - and build confusion matrix tp=length(find(cl(1:5)==1)); fp=length(find(cl(6:10)==1)); fn=length(find(cl(1:5)==2)); tn=length(find(cl(6:10)==2)); confmat=[tp fn; fp tn] % ninc=200; [pltx,plty]=finddecisionboundary(train(1:20,:),train(21:40,:),1,2,ninc) % plot all of the points that were on the decision boundary hold on plot(pltx,plty,'k-'); hold off