Homework 5 Data The data for this homework is dataset_4. Please

advertisement
Homework 5
Data
The data for this homework is dataset_4. Please see homework 4 for details.
1. See homework 4 solution (point 3)
2. Do pair-wise classification between activity patterns using the prewhitened estimates
from (1). That is, you need to do 10 classifications for each hand, each finger against
each finger. Report the classification accuracies for each pair. Visualize the distance
measures by presenting them in a 5x5 Representational Dissimilarity Matrix (RDM).
Repeat the process for the ipsilateral, right hand.
ANSWER: See Figure 1 A,B.
Contralateral classification accuracies:
1-2: 1.0000
1-3: 1.0000
1.0000
!
1.0000
!
1.0000
!
1.0000
!
0.9375
!
1.0000
!
1.0000
ipsilateral:
0.5625
0.8750
1.0000
0.8750
0.6250
0.6875
0.8125
0.8125
0.4375
0.6875
3. Calculate the 10 pairwise Euclidean distances between the mean activity patterns for
each finger. Use non-cross-validated distances.
MATLAB Tip: The functions pdist and squareform can make your life much easier.
ANSWER: See Figure 1, C,D
Contralateral distances:
0.9106
1.2368
1.2268
1.0527
0.8155
0.8973
0.9448
0.6683
0.7787
0.6762
Ipsilateral distances:
0.5315
0.6876
0.7197
0.6560
0.5429
0.5940
0.5663
0.5868
0.5057
0.5691
4. Make a scatterplot of the classification accuracies against the Euclidean distances for
the left and the right hand separately. What do you observe?
Answer: Figure 1 e,f
For the contralateral side, classification accuracies saturate at 100%.
Classification contra
Classification ipsi
1
1
2
2
3
3
4
4
5
5
1
2
3
4
5
1
Distance contra
1
2
2
3
3
4
4
5
5
3
4
5
1
1
1
0.99
0.9
classification accuracy
classification accuracy
2
0.98
0.97
0.96
0.95
0.94
0.93
0.6
0.8
1
distance
1.2
3
4
5
Distance ipsi
1
1
2
1.4
2
3
4
5
0.8
0.7
0.6
0.5
0.4
0.5
0.6
0.7
0.8
distance
Figure 1. Panels from upper left to lower right: (a) Classification accuracy for contralateral
hand. (b) Classification accuracy for ipsilateral hand. (c) Euclidean distance for contra
lateral hand. (d) Euclidean distance for ipsilateral hand. (e) Scatterplot of Euclidean
distance and classification accuracy contralateral and (f) ipsilateral.
function varargout = homework5(what,varargin)
% Example matlab script to solve homework4
%
%
run=[]; % Run is a variable
switch(what)
case '1_prewhiten'
nRuns =8;
load dataset_4.mat;
B=[];
% Estimate Betas and residuals - plot Sigma estimates
for r=1:nRuns
X=[Xtask(:,:,r) Xhpf(:,:,r) Xintercept(:,:,r)];
B(:,:,r)=pinv(X)*Y(run==r,:);
R(:,:,r)=Y(run==r,:)-X*B(:,:,r);
Sig(:,:,r) = R(:,:,r)'*R(:,:,r);
end;
% Average and regularize
Sigma = mean(Sig,3);
Sigma = Sigma + eye(size(Sigma))*mean(diag(Sigma))*0.01; % Add 1 % to
diagonal
% Prewhiten the data
for r=1:nRuns
B(:,:,r)=B(:,:,r)*Sigma^(-0.5);
doing this
end;
% There are faster ways of
varargout = {B};
case 'calculate_distances'
% Redo the leave-one-out normalisation
B = homework5('1_prewhiten');
hand = {'contra','ipsi'};
for h=1:2
handIndx=[1:5]+(h-1)*5;
% For Left hand
H=B(handIndx,:,:);
% Get the pairwise classification accuracies
for a=1:5
for b=1:5
if (a~=b)
acc(a,b) = nn_classifier(H([a b],:,:));
end;
end;
end;
accV = squareform(acc);
% Get simple Euclidean distances
% Because we are on prewhitened data this is the Mahalanbis distance
distV = pdist(mean(H,3),'euclidean'); % Calculate distances on the
quick
subplot(3,2,h);
imagesc(squareform(accV));
title(sprintf('Classification %s',hand{h}));
subplot(3,2,2+h);
imagesc(squareform(distV));
title(sprintf('Distance %s',hand{h}));
subplot(3,2,4+h);
plot(distV,accV,'k.');
ylabel('classification accuracy');
xlabel('distance');
[accV' distV']
end;
end;
function acc=nn_classifier(data);
nPart = size(data,3); % Partitions are the 3 dimension
nCond = size (data,1); % Number of conditons
part = [1:nPart];
for n=1:nPart
trainIndx = find(part~=n);
testIndx = find(part==n);
Mu_hat = mean(data(:,:,trainIndx),3); % Calculate the training means
% Now classify
for c=1:nCond
x = data(c,:,testIndx); % This is the test pattern
dist=x*x'-2*Mu_hat*x'+sum(Mu_hat.^2,2);
[~,k(c,n)]=min(dist); % Record the classification
end;
end;
% Caluclate the % correct
correct=bsxfun(@eq,k,[1:nCond]');
acc = sum(correct(:))/numel(correct(:));
Download