%% matlab_demo_11.m
%%
%% CSS 490/590, Introduction to Machine Learning, Winter 2012
%% Computing & Software Systems, University of Washington Bothell
%% written by J. Jeffry Howbert, 2012-02-15
%%
%% visualization of k-means clustering on MNIST dataset

clear all;
close all;
load mnistabridged;

tic;

data = double( train );
label = double( trainlabels );
distance = squareform( pdist( data ) );

% initial centroids chosen randomly, so important to set RandStream
randSeed = 1;
RandStream.setDefaultStream( RandStream( 'mt19937ar', 'Seed', randSeed ) );

k = 10;
clusIdx = kmeans( data, k );

nPlotRow = floor( sqrt( k ) );
nPlotCol = ceil( sqrt( k ) );
while ( nPlotRow * nPlotCol < k )
    nPlotCol = nPlotCol + 1;
end
fprintf( 1, 'cluster    size' );
for la = 0 : 9
    fprintf( 1, '  %3d', la );
end
fprintf( 1, '\n' );
figure( 1 );
for cl = 1 : k      % for each cluster
    % pull out all datapoints that were assigned to cluster cl 
    dataClus = data( clusIdx == cl, : );
    % print size of cluster and number of each true label in cluster 
    fprintf( 1, '   %2d       %d', cl, size( dataClus, 1 ) );
    for la = 0 : 9
        fprintf( 1, '  %3d', sum( label( clusIdx == cl ) == la ) );
    end
    fprintf( 1, '\n' );
    % get mean of all datapoints in cluster cl
    meanDataClus = mean( dataClus );
    % reshape pixels from linearized vector to square image form 
    meanDataClus = reshape( meanDataClus, 28, 28 );
    subplot( nPlotRow, nPlotCol, cl );
    imagesc( meanDataClus );
    colormap( gray );
end

% display original unordered distance matrix
figure( 2 );
imagesc( distance );
colorbar;

% display distance matrix ordered by cluster membership
figure( 3 );
[ clusIdx, sampIdx ] = sort( clusIdx );
distOrd = distance( sampIdx, sampIdx );
imagesc( distOrd );
colorbar;

toc;
