Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding Confusionmatrix and AUC figures
- Loading branch information
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
clear all | ||
close all | ||
addpath(genpath('..')) | ||
|
||
%% Loading dataset | ||
|
||
dataset='MI'; | ||
subject=4; | ||
[xapp,yapp,xtest,ytest]=get_dataset(dataset,subject); | ||
|
||
%xapp=xapp(:,[1 6]); | ||
%xtest=xtest(:,[1 6]); | ||
|
||
%% Dataset visualization | ||
|
||
figure(1) | ||
i1=1; | ||
i2=2; | ||
plot(xapp(yapp==1,i1),xapp(yapp==1,i2),'+r') | ||
hold on | ||
plot(xapp(yapp==-1,i1),xapp(yapp==-1,i2),'x') | ||
hold off | ||
title('MI dataset training') | ||
legend('Class +1','Class -1') | ||
xlabel(['feature ' num2str(i1)]) | ||
ylabel(['feature ' num2str(i2)]) | ||
|
||
|
||
%% LDA classifier + visu | ||
|
||
lambda=1e0; | ||
[w,w0]=ldaclass(xapp,yapp,lambda) | ||
|
||
ypred_app=xapp*w+w0; | ||
ypred_test=xtest*w+w0; | ||
|
||
ACC_app=mean(sign(ypred_app)==yapp) | ||
ACC_test=mean(sign(ypred_test)==ytest) | ||
|
||
[AUC,tpr,fpr,b]=svmroccurve(ypred_test,ytest); | ||
AUC | ||
|
||
%% SVM classifier + visu | ||
|
||
lambda=1e0; | ||
[w2,w02]=lsvmclass(xapp,yapp,lambda) | ||
|
||
ypred_app=xapp*w2+w02; | ||
ypred_test=xtest*w2+w02; | ||
|
||
ACC2_app=mean(sign(ypred_app)==yapp) | ||
ACC2_test=mean(sign(ypred_test)==ytest) | ||
|
||
[AUC2,tpr2,fpr2,b2]=svmroccurve(ypred_test,ytest); | ||
%% plot discriminant function | ||
|
||
lw=2; | ||
fs=16; | ||
|
||
figure(2) | ||
set(gcf,'defaulttextinterpreter','latex'); | ||
plot(fpr,tpr) | ||
hold on | ||
plot(fpr2,tpr2,'r') | ||
%plot(xapp(yapp==-1,1),xapp(yapp==-1,2),'') | ||
%ylim([9 12.5]) | ||
plot([0 1],[0 1],'k') | ||
hold off | ||
title('Classification Imagerie Motrice','FontSize',fs) | ||
|
||
l=legend(['LDA, AUC=' num2str(AUC,3) ' \hspace{2mm}'],['SVM, AUC=' num2str(AUC2,3) ' '],'Classif. al\''eatoire','Location','SouthEast'); | ||
set(l,'FontSize',fs,'interpreter','latex') | ||
xlabel(['FPR'],'FontSize',fs) | ||
ylabel(['TPR'],'FontSize',fs) | ||
print('-depsc','visu_AUC_MI.eps') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
clear all | ||
close all | ||
addpath(genpath('..')) | ||
|
||
%% Loading dataset | ||
|
||
dataset='P300'; | ||
[xapp,yapp,xtest,ytest]=get_dataset(dataset); | ||
|
||
%xapp=xapp(:,[1 6]); | ||
%xtest=xtest(:,[1 6]); | ||
|
||
|
||
|
||
%% LDA classifier + visu | ||
|
||
lambda=1e0; | ||
[w,w0]=ldaclass(xapp,yapp,lambda) | ||
|
||
ypred_app=xapp*w+w0; | ||
ypred_test=xtest*w+w0; | ||
|
||
ACC_app=mean(sign(ypred_app)==yapp) | ||
ACC_test=mean(sign(ypred_test)==ytest) | ||
|
||
[AUC,tpr,fpr,b]=svmroccurve(ypred_test,ytest); | ||
AUC | ||
|
||
%% SVM classifier + visu | ||
|
||
lambda=1e3; | ||
[w2,w02]=lsvmclass(xapp,yapp,lambda) | ||
|
||
ypred_app=xapp*w2+w02; | ||
ypred_test=xtest*w2+w02; | ||
|
||
ACC2_app=mean(sign(ypred_app)==yapp) | ||
ACC2_test=mean(sign(ypred_test)==ytest) | ||
|
||
[AUC2,tpr2,fpr2,b2]=svmroccurve(ypred_test,ytest); | ||
%% plot discriminant function | ||
|
||
lw=2; | ||
fs=16; | ||
|
||
figure(2) | ||
set(gcf,'defaulttextinterpreter','latex'); | ||
plot(fpr,tpr) | ||
hold on | ||
plot(fpr2,tpr2,'r') | ||
%plot(xapp(yapp==-1,1),xapp(yapp==-1,2),'') | ||
%ylim([9 12.5]) | ||
plot([0 1],[0 1],'k') | ||
hold off | ||
title('Classification P300 Speller','FontSize',fs) | ||
|
||
l=legend(['LDA, AUC=' num2str(AUC,3) ' \hspace{2mm}'],['SVM, AUC=' num2str(AUC2,3) ' '],'Classif. al\''eatoire','Location','SouthEast'); | ||
set(l,'FontSize',fs,'interpreter','latex') | ||
xlabel(['FPR'],'FontSize',fs) | ||
ylabel(['TPR'],'FontSize',fs) | ||
print('-depsc','visu_AUC_MI.eps') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function [C,metric]=ConfusionMatrix(ypred,ytrue,classcode) | ||
% [C,metric]=ConfusionMatrix(ypred,ytrue,classcode) | ||
% | ||
% ypred and ytrue -1, 1 | ||
% if classcode = 1 -1 | ||
% | ||
% TP FN | ||
% FP TN | ||
|
||
|
||
C=zeros(2); | ||
N=length(classcode); | ||
for i=1:N | ||
for j=1:N | ||
C(i,j)= length( find(ypred==classcode(j) & ytrue == classcode(i))); | ||
end; | ||
end; | ||
nbpos=sum(ytrue==1); | ||
nbneg=sum(ytrue==-1); | ||
c=nbneg/nbpos; | ||
if N==2 | ||
metric.detection=C(1,1)/sum(C(1,:)); | ||
|
||
if sum(C(:,1))~=0 | ||
metric.precision=C(1,1)/(C(1,1)+C(2,1)); | ||
else | ||
metric.precision=NaN; | ||
end; | ||
metric.recall=C(1,1)/(C(1,1)+C(1,2)); | ||
metric.fmeasure=2*C(1,1)/(C(1,1)+C(2,1)+nbpos); | ||
metric.wracc=(4*c)/(1+c)^2*(C(1,1)-C(2,1)); | ||
else | ||
for i=1:N | ||
metric.accuracybyclass(i)=C(i,i)/sum(C(i,:)); | ||
metric.falsedetectionbyclass(i)=(sum(C(:,i))-C(i,i))/sum(C(:,i)); | ||
end | ||
|
||
end | ||
metric.accuracy=sum(diag(C))/sum(sum(C)); |