File size: 3,686 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
function Classify_Scalars_SVM(InData,TITLE,iterations,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

Aset_score=NaN(28,iterations);
Bset_score=NaN(28,iterations);
for rep = 1:iterations
    if rem(rep,100)==0
        ['SVM   '  Xval '   '   TITLE  '   '  num2str(rep)]
    end

    % X-Validation: Equate epochs
    size_targs=size(Big_A,1); %  -------- "TARGETS" are Patient
    size_stds=size(Big_B,1);  %  -------- "STANDARDS" are CTL
    if size_targs<size_stds
        temp=shuffle(1:size_stds);
        TARGETS=Big_A;
        STANDARDS=Big_B(temp(1:size_targs),:);  clear temp size_stds size_targs;
    elseif size_stds<size_targs
        temp=shuffle(1:size_targs);
        TARGETS=Big_A(:,:,temp(1:size_stds));  clear temp size_stds size_targs;
        STANDARDS=Big_B;
    else
        TARGETS=Big_A;
        STANDARDS=Big_B;
    end
    AllData = cat(1,TARGETS,STANDARDS);
    GROUPS = [ones(1,size(TARGETS,1)),zeros(1,size(STANDARDS,1))];
    Size_Per_Set = size(AllData,1) .* .5;
    
    % Cross Validate
    if     strmatch(Xval,'5X');  testsize = floor(.2*Size_Per_Set);
    elseif strmatch(Xval,'10X'); testsize = floor(.1*Size_Per_Set);
    elseif strmatch(Xval,'LOO'); testsize = 1;
    end
    trainsize = Size_Per_Set-testsize;
    % For TARGETS
    ForRand = shuffle(1:Size_Per_Set);
    TrainBool_T = zeros(1,Size_Per_Set); TrainBool_T(ForRand(1:trainsize))=1;
    Test1Bool_T = zeros(1,Size_Per_Set); Test1Bool_T(ForRand(trainsize+1:trainsize+testsize))=1;
    % For STANDARDS
    ForRand = shuffle(1:Size_Per_Set);
    TrainBool_S = zeros(1,Size_Per_Set); TrainBool_S(ForRand(1:trainsize))=1;
    Test1Bool_S = zeros(1,Size_Per_Set); Test1Bool_S(ForRand(trainsize+1:trainsize+testsize))=1;
    % same for standards and targets
    TrainBool = [TrainBool_T TrainBool_S];
    Test1Bool = [Test1Bool_T Test1Bool_S];
    
    % 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(find(Test1Bool_T==1),rep)=score(1:length(acc)/2,1);
    Bset_score(find(Test1Bool_S==1),rep)=score((length(acc)/2)+1:length(acc),1);
    % ############# SVM #############
    
    clearvars -except Aset_acc Bset_acc Aset_score Bset_score rep Big_A Big_B InData Xval TITLE iterations
    
end

% Save
save(['SVM_',TITLE,'_',Xval,'_iter',num2str(iterations),'.mat'],'Aset_acc','Bset_acc','Aset_score','Bset_score');