File size: 2,559 Bytes
d7989aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
function Classify_Scalars_SVM_MatchSubjs(InData,TITLE,MATCHTOEEG,vars,Xval)
% Nomenclature is that 'A' TARGETS are classified (1's) from 'B' STANDARDS (0's)

Big_A=InData(InData(:,1)==1,vars) ;  % PD
Big_B=InData(InData(:,1)~=1,vars) ;  % CTL
% Align them so each CTL matches each PD
Big_B=Big_B(MATCHTOEEG',:);



for rep = 1:28

    if rem(rep,100)==0

        ['SVM   '  Xval '   '   TITLE  '   '  num2str(rep)]

    end



    AllData = cat(1,Big_A,Big_B);

    GROUPS = [ones(1,size(Big_A,1)),zeros(1,size(Big_B,1))];

    Size_Per_Set = size(AllData,1) .* .5;

    

    % Cross Validate

    testsize = 1;

    trainsize = Size_Per_Set-testsize;

    % get shuffled trials for train set

    TrainBool = ones(1,Size_Per_Set); TrainBool(rep)=0;

    % get shuffled trials for validate set

    Test1Bool = zeros(1,Size_Per_Set); Test1Bool(rep)=1;

    % same for standards and targets

    TrainBool = [TrainBool TrainBool];

    Test1Bool = [Test1Bool Test1Bool];

    

    % Classify ****** Targets ******  



    % training set

    x_train = AllData(TrainBool==1,:);

    % normalize!

    x_mean = mean(x_train); x_std = std(x_train);

    x_train_norm = (x_train - repmat(x_mean,size(x_train,1),1));  % mean normalize

    x_train_norm = x_train_norm./repmat(x_std,size(x_train,1),1);

    y_train = GROUPS(TrainBool==1);

    

    % validate set

    xtstset1 = AllData(Test1Bool == 1,:);

    % normalize to train set

    xtstset1_norm = (xtstset1-repmat(x_mean,size(xtstset1,1),1));

    xtstset1_norm = xtstset1_norm./ repmat(x_std,size(xtstset1,1),1); % normalize to trn data

    y_tstset1 = GROUPS(Test1Bool==1);

    

    % store normalizing constants. Important to normalize to that when applying

    % weights to a different data set later.

    norm{1}(rep,:) = x_mean;

    norm{2}(rep,:) = x_std;

   

    x_train = x_train_norm;

    x_test1 = xtstset1_norm;

    

    % ############# SVM #############

    SVMModel = fitcsvm(x_train,y_train','KernelFunction','linear');
    [label,score] = predict(SVMModel,x_test1);
    
    acc=label==y_tstset1';

    Aset_acc(rep,:)=acc(1:length(acc)/2);

    Bset_acc(rep,:)=acc((length(acc)/2)+1:end);

    

    Aset_score(rep,:)=score(1,1);

    Bset_score(rep,:)=score(2,1);

    % ############# SVM #############

    

    clearvars -except Aset_acc Bset_acc Aset_score Bset_score rep Big_A Big_B InData Xval TITLE iterations MATCHTOEEG

    

end



% Save

save(['SVM_',TITLE,'_',Xval,'_iter28','.mat'],'Aset_acc','Bset_acc','Aset_score','Bset_score');