%% matlab_demo_07.m
%%
%% CSS 490/590, Introduction to Machine Learning, Winter 2012
%% Computing & Software Systems, University of Washington Bothell
%% written by J. Jeffry Howbert, 2012-01-31

%% Part A
%% demo of nearest neighbor classification on MNIST dataset
clear all;
close all;
load mnistabridged;

trainData = double( train );
testData = double( test );
trainLabel = double( trainlabels );
testLabel = double( testlabels );
nTrain = length( trainlabels );
nTest = length( testlabels );

% %%&& for test only
% nTestSub = 100;
% testData = testData( 1 : nTestSub, : );
% testLabel = testLabel( 1 : nTestSub );
% nTest = length( testLabel );
% %%&& end for test only

% testPredIdx = knnsearch( trainData, testData );   %%&& knnsearch() doesn't exist in R2009a!!
% testPred = trainLabel( testPredIdx );

tic;

% poor man's k-nearest neighbor
testMinDistIdx = zeros( 1, nTest );
for ix = 1 : nTest
    % get difference between vector of test sample and all training samples
    diff = trainData - repmat( testData( ix, : ), nTrain, 1 );
    % element-wise squares of difference vector
    diff2 = diff .^ 2;
    % add up squares to get square of Euclidean difference - don't need to
    % take square root
    sumDiff2 = sum( diff2, 2 );
    [ testMinDist, testMinDistIdx( ix ) ] = min( sumDiff2 );
end
testPred = trainLabel( testMinDistIdx );

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

toc;

%% Part B
%% various methods of regression on prostate cancer data
% use this section for all regression methods
clear all;
close all;
load prostate;
nTrain = size( trainData, 1 );
nTest = size( testData, 1 );

% % standardize each column of training data to 0 mean and variance 1 (optional)
% [ trainData, mu, sigma ] = zscore( trainData );
% % also apply mu and sigma from training zscore to test data
% testData = testData - repmat( mu, size( testData, 1 ), 1 );
% testData = testData ./ repmat( sigma, size( testData, 1 ), 1 );

% linear regression
% add column of ones so model will have intercept
trainData = ( [ ones( size( trainData, 1 ), 1 ), trainData ] );
testData = ( [ ones( size( testData, 1 ), 1 ), testData ] );
beta = mvregress( trainData, trainResponse );
trainPred = trainData * beta;
testPred = testData * beta;

% % regression tree
% tr = classregtree( trainData, trainResponse, 'method', 'regression' );
% % trained tree has 9 pruning levels
% pruneLevel = 0;
% trainPred = eval( tr, trainData, pruneLevel );
% testPred = eval( tr, testData, pruneLevel );

% use this section for all regression methods
meanSqErrorTrain = sum( ( trainPred - trainResponse ) .^ 2 ) / nTrain;
fprintf( 1, 'mean squared error for training samples = %8.4f\n', meanSqErrorTrain ); 
meanSqErrorTest = sum( ( testPred - testResponse ) .^ 2 ) / nTest;
fprintf( 1, 'mean squared error for test samples     = %8.4f\n', meanSqErrorTest ); 
