-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkMGM.m
190 lines (124 loc) · 4.58 KB
/
checkMGM.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
close all;
clear;
clc;
%%
% subjects = [1];
% sessions = [1];
table_to_show = [];
table_to_show = [...
"name" ,...
"worst precision" ,...
"best precision" ,...
"pca " ,...
"pca GMM precision" ,...
];
subjects = 1:1;
sessions = 1:1;
subject = 2;
sess = 1;
[Events, vClass] = getERPEvents(subject, sess);
%% Original Data
Events_with_mean = addDiffrenceAverage( Events, vClass);
Events_with_pca = addPCAAverage( Events );
%%
c_data_for_classifier = {};
c_description_for_data = {};
funcs = { @covFromCellArrayOfEvents};
funcs_names = { "Covariance"};
tmp_description = "subject " + num2str(subject) + " session " + num2str(sess);
events_names = {
"original session" + tmp_description,...
"new session" + tmp_description,...
};
events_cell = {
Events ,...
Events_with_pca ,...
};
%set base functions
% all_base_functions = ["linear", "gaussian", "polynomial"];
all_base_functions = ["linear"];
%extract the features
[c_data_for_classifier, c_description_for_data] = extractFeatures( events_cell, events_names,...
funcs, funcs_names );
%% - show pca
figure();
subplot(3, 3, 4);
showPCA(c_data_for_classifier{1}, vClass);
title('PCA original');
subplot(3, 3, 5);
showPCA(c_data_for_classifier{2}, vClass);
title('PCA with mean');
%%
% t = templateSVM('Standardize' , false,...
% 'KernelFunction', 'linear');
%
% Mdl = fitcecoc( c_data_for_classifier{2}', ...
% vClass , ...
% 'Learners' , ...
% t);
%
% CMdl = compact(Mdl);
% avr_loss = crossval(CMdl)
input_data = c_data_for_classifier{2};
input_lable = vClass;
t = templateSVM('Standardize', false, 'KernelFunction', 'linear');
Mdl = fitcecoc( input_data', ...
input_lable, ...
'KFold', 10, ...
'Learners', t);
avr_loss = 1 - kfoldLoss(Mdl)
%%
%%
function [mean_1] = getMean(cInput, vClass)
%ADDAVERAGE Summary of this function goes here
% Detailed explanation goes here
%calc mean of vClass == 1
mat = cat(3, cInput{:});
mean_1 = mean(mat(:, :, vClass==2 ), 3);
end
function [] = showTSNE(flattened_cov, vClass)
tsne_points = tsne(flattened_cov');
% firsst session
class_1 = 1;
class_2 = 2;
scatter( tsne_points(vClass == class_1, 1), tsne_points(vClass == class_1, 2), 30, 'r', 'filled', 'MarkerEdgeColor', 'k' );
hold on;
scatter( tsne_points(vClass == class_2, 1), tsne_points(vClass == class_2, 2), 30, 'b', 'filled', 'MarkerEdgeColor', 'k' );
hold on;
% legend('not target', 'target');
end
function [] = showPCA(flattened_cov, vClass)
eigen_vectors = pca(flattened_cov');
two_components = (flattened_cov' * eigen_vectors(:, 1:2));
% firsst session
class_1 = 1;
class_2 = 2;
scatter( two_components(vClass == class_1, 1), two_components(vClass == class_1, 2), 30, 'r', 'filled', 'MarkerEdgeColor', 'k' );
hold on;
scatter( two_components(vClass == class_2, 1), two_components(vClass == class_2, 2), 30, 'b', 'filled', 'MarkerEdgeColor', 'k' );
hold on;
% legend('not target', 'target');
end
function [pre1, pre2] = calPrecision(X, X_test,y, y_test, data_name)
t = templateSVM('Standardize', false, 'KernelFunction', 'linear');
Mdl = fitcecoc( X', ...
y, ...
'Learners', t);
predicted_label = predict( Mdl, X_test' );
% calc precision
tp = sum((y_test == 2) & (predicted_label == 2));
fp = sum((y_test == 1) & (predicted_label == 2));
if tp + fp ~= 0
pre1 = tp / (tp + fp);
else
pre1 = 0;
end
%calc recal
tn = sum((y_test == 1) & (predicted_label == 1));
fn = sum((y_test == 2) & (predicted_label == 1));
if tn + fn ~= 0
pre2 = tn / (tn + fn);
else
pre2 = 0;
end
end