%% matlab_demo_06.m
%%
%% CSS 581, Introduction to Machine Learning, Winter 2014
%% Computing & Software Systems, University of Washington Bothell
%% written by J. Jeffry Howbert, 2012-01-25
%%
%% demo of classify function for discriminant analysis

clear all

% %% ***** use Fisher iris dataset
% load fisheriris
% 
% rng( 1 );
% nInst = size( meas, 1 );
% rp = randperm( nInst );
% nTrain = 100;
% nTest = nInst - nTrain;
% 
% label = grp2idx( species );
% trainDat = meas( rp( 1 : nTrain ), : );
% trainLabel = label( rp( 1 : nTrain ) );
% testDat = meas( rp( nTrain + 1 : end ), : );
% testLabel = label( rp( nTrain + 1 : end ) );
% %% *****

% %% ***** use heart disease dataset
% load heart
% 
% rng( 1 );
% nInst = size( dat, 1 );
% rp = randperm( nInst );
% nTrain = 200;
% nTest = nInst - nTrain;
% 
% trainDat = dat( rp( 1 : nTrain ), : );
% trainLabel = label( rp( 1 : nTrain ) );
% testDat = dat( rp( nTrain + 1 : end ), : );
% testLabel = label( rp( nTrain + 1 : end ) );
% %% *****

%% ***** use MNIST handwritten digits dataset
load mnistabridged
rng( 1 );
trainDat = double( train );
trainLabel = double( trainlabels );
testDat = double( test );
testLabel = double( testlabels );
nTrain = size( trainDat, 1 );
nTest = size( testDat, 1 );

%% This dataset give an error:
%%      ??? Error using ==> classify at 246
%%      The pooled covariance matrix of TRAINING must be positive definite.
%% What's going on?  The following gives a bit of insight: 
imagesc( cov( trainDat ) == 0 );
colormap( gray );
%% *****

% Type of discriminant function.  Allowed values: 'linear' (default),
%   'diaglinear', 'quadratic', 'diagquadratic', 'mahalanobis'
type = 'linear';

trainPred = classify( trainDat, trainDat, trainLabel, type );
testPred = classify( testDat, trainDat, trainLabel, type );

nTrainCorrect = sum( trainLabel == trainPred );
nTestCorrect = sum( testLabel == testPred );
fprintf( 1, 'correct predictions on training set :   %d / %d,  %5.2f%%\n', ...
    nTrainCorrect, nTrain, 100 * nTrainCorrect / nTrain );
[ mat, order ] = confusionmat( trainLabel, trainPred );
disp( ' ' );
disp( order' );
disp( mat );
fprintf( 1, 'correct predictions on test set     :   %d / %d,  %5.2f%%\n', ...
    nTestCorrect, nTest, 100 * nTestCorrect / nTest );
[ mat, order ] = confusionmat( testLabel, testPred, 'order', order );
disp( ' ' );
disp( order' );
disp( mat );
