-
Notifications
You must be signed in to change notification settings - Fork 0
/
LDA_simplest_test.m
118 lines (109 loc) · 5.42 KB
/
LDA_simplest_test.m
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
function [acc, W, bias] = LDA_simplest_test(clf, exp)
addpath('/Users/scottyang/Desktop/EEG/MemoryFinale/LOSO/Exp1')
% use several conditions to specified the desired experiments and classifier.
if exp == 1
load 'data_imbalLDA_1.mat'
tr_order = user_tr_order_1;
proj_score = user_prob_1;
source_label = user_source_1;
resp_label = user_resp_1;
behav_feat = user_feat_1;
if clf == "SN_vs_MN"
% iteratively aggregate user's data to form the huge trainning set
for user = 1:length(proj_score)
train_SNMN_cm = cell(1,2);
pos1_idx = source_label{user} == 2 & resp_label{user}==5;
neg1_idx = source_label{user} == 2 & resp_label{user}==4;
pos2_idx = source_label{user} == 4 & resp_label{user}==5;
neg2_idx = source_label{user} == 4 & resp_label{user}==4;
pos_idx = [behav_feat{user}(pos1_idx,:);behav_feat{user}(pos2_idx,:)];
neg_idx = [behav_feat{user}(neg1_idx,:);behav_feat{user}(neg2_idx,:)];
train_SNMN_cm{1} = cat(1, train_SNMN_cm{1}, pos_idx);
train_SNMN_cm{2} = cat(1, train_SNMN_cm{2}, neg_idx);
if user == 1
train_set = cat(1, train_SNMN_cm{1}, train_SNMN_cm{2});
train_y = [ones(size(train_SNMN_cm{1},1),1); -1*ones(size(train_SNMN_cm{2},1),1)];
else
train_set = cat(1, train_SNMN_cm{1}, train_SNMN_cm{2}, train_set);
sub_y = [ones(size(train_SNMN_cm{1},1),1); -1*ones(size(train_SNMN_cm{2},1),1)];
train_y = cat(1, sub_y, train_y);
end
end
else
for user = 1:length(proj_score)
train_SNMN_cm = cell(1,2);
pos1_idx = source_label{user} == 1 & resp_label{user}==3;
neg1_idx = source_label{user} == 2 & resp_label{user}==4;
pos2_idx = source_label{user} == 3 & resp_label{user}==3;
neg2_idx = source_label{user} == 2 & resp_label{user}==5;
pos_idx = [behav_feat{user}(pos1_idx,:);behav_feat{user}(pos2_idx,:)];
neg_idx = [behav_feat{user}(neg1_idx,:);behav_feat{user}(neg2_idx,:)];
train_SNMN_cm{1} = cat(1, train_SNMN_cm{1}, pos_idx);
train_SNMN_cm{2} = cat(1, train_SNMN_cm{2}, neg_idx);
if user == 1
train_set = cat(1, train_SNMN_cm{1}, train_SNMN_cm{2});
train_y = [ones(size(train_SNMN_cm{1},1),1); -1*ones(size(train_SNMN_cm{2},1),1)];
else
train_set = cat(1, train_SNMN_cm{1}, train_SNMN_cm{2}, train_set);
sub_y = [ones(size(train_SNMN_cm{1},1),1); -1*ones(size(train_SNMN_cm{2},1),1)];
train_y = cat(1, sub_y, train_y);
end
end
end
% exp 2
else
load 'data_imbalLDA_2.mat'
tr_order = user_tr_order_2;
proj_score = user_prob_2;
source_label = user_source_2;
resp_label = user_resp_2;
behav_feat = user_feat_2;
%%
if clf == "SN_vs_MN"
for user = 1:length(proj_score)
train_SNMN_cm = cell(1,2);
pos1_idx = source_label{user} == 2 & resp_label{user}==5;
neg1_idx = source_label{user} == 2 & resp_label{user}==4;
pos2_idx = source_label{user} == 4 & resp_label{user}==5;
neg2_idx = source_label{user} == 4 & resp_label{user}==4;
pos_idx = [behav_feat{user}(pos1_idx,:);behav_feat{user}(pos2_idx,:)];
neg_idx = [behav_feat{user}(neg1_idx,:);behav_feat{user}(neg2_idx,:)];
train_SNMN_cm{1} = cat(1, train_SNMN_cm{1}, pos_idx);
train_SNMN_cm{2} = cat(1, train_SNMN_cm{2}, neg_idx);
if user == 1
train_set = cat(1, train_SNMN_cm{1}, train_SNMN_cm{2});
train_y = [ones(size(train_SNMN_cm{1},1),1); -1*ones(size(train_SNMN_cm{2},1),1)];
else
train_set = cat(1, train_SNMN_cm{1}, train_SNMN_cm{2}, train_set);
sub_y = [ones(size(train_SNMN_cm{1},1),1); -1*ones(size(train_SNMN_cm{2},1),1)];
train_y = cat(1, sub_y, train_y);
end
end
else
for user = 1:length(proj_score)
train_SNMN_cm = cell(1,2);
pos1_idx = source_label{user} == 1 & resp_label{user}==3;
neg1_idx = source_label{user} == 2 & resp_label{user}==4;
pos2_idx = source_label{user} == 3 & resp_label{user}==3;
neg2_idx = source_label{user} == 2 & resp_label{user}==5;
pos_idx = [behav_feat{user}(pos1_idx,:);behav_feat{user}(pos2_idx,:)];
neg_idx = [behav_feat{user}(neg1_idx,:);behav_feat{user}(neg2_idx,:)];
train_SNMN_cm{1} = cat(1, train_SNMN_cm{1}, pos_idx);
train_SNMN_cm{2} = cat(1, train_SNMN_cm{2}, neg_idx);
if user == 1
train_set = cat(1, train_SNMN_cm{1}, train_SNMN_cm{2});
train_y = [ones(size(train_SNMN_cm{1},1),1); -1*ones(size(train_SNMN_cm{2},1),1)];
else
train_set = cat(1, train_SNMN_cm{1}, train_SNMN_cm{2}, train_set);
sub_y = [ones(size(train_SNMN_cm{1},1),1); -1*ones(size(train_SNMN_cm{2},1),1)];
train_y = cat(1, sub_y, train_y);
end
end
end
end
disp("The shape of the training set is")
disp(size(train_set))
[acc, ~, ~, ~, W, ~, ~, bias] = lda_study_prob(train_set, train_set, train_y, train_y, 1, 3);
out_msg = strcat("the accuracy for experiment " + exp + " " + clf + " is: " + acc);
disp(out_msg)
end