-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnFold_AnyClassifier_withFeatureselection_v3_perpatient.m
271 lines (236 loc) · 10.9 KB
/
nFold_AnyClassifier_withFeatureselection_v3_perpatient.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
%log
% v3 use socring for feature ranking
% example here:
% para.feature_score_method='weighted';
% para.classifier='QDA';
% para.num_top_feature=5;
% para.featureranking='wilcoxon';
% para.correlation_factor=.9;
% [resultImbalancedC45,feature_scores] = nFold_AnyClassifier_withFeatureselection_v3(data_all_w,labels,feature_list_t,para,1,intFolds,intIter);
% this function is not finished
function [stats,stats_patlevel, feature_scores]= nFold_AnyClassifier_withFeatureselection_v3_perpatient(data_set,data_labels,patient_labels,feature_list,para,shuffle,n,nIter,Subsets)
% Using n-fold subset selection and C4.5 decision tree classifier
% Input:
% data_set: data
% data_labels: labels
% feature_list: the feature name list in cell
% para:
% parameter like what classifier you use, the number of top feature
% para.classifier='LDA';
% para.num_top_feature=5;
% shuffle: 1 for random, 0 for non-random partition (Default: 1)
% n: Number of folds to your cross-validation (Default: 3)
% nIter: Number of cross-validation iterations (Default: 1)
% Subsets: pass your own training and testing subsets & labels (Default:
% computer will generate using 'nFold')
%
% Output:
% stats: struct containing TP, FP, TN, FN, etc.
% The function is written by Cheng Lu @2016
% example:
% para.classifier='LDA';
% para.num_top_feature=5;
% [resultImbalancedC45,feature_scores] = nFold_AnyClassifier_withFeatureselection(data_all_w,labels,feature_list_t,para,1,intFolds,intIter);
% data_set = eigVec_graphfeats;
% data_labels = labels(:);
% classifier = 'SVM';
% shuffle = 1;
% n = 3;
if nargin < 8+1
Subsets = {};
end
if nargin < 7+1
nIter = 1;
end
if nargin < 6+1
n = 4; % 3-fold cross-validation
end
if nargin < 5+1
shuffle = 1; % randomized
end
% if any(~xor(data_labels == 1, data_labels == -1)), error('Labels must be 1 and -1'); end
feature_scores=zeros(size(data_set,2),1);
% get unique patient labels and corresponding data labels
[unique_patient_labels, idx, ~] = unique(patient_labels,'first');
data_labels_patlevel = data_labels(idx);
stats = struct; %cell(1,nIter);
for j=1:nIter
fprintf('Iteration: %i\n',j);
% reset total statistics
Ttp = 0; Ttn = 0; Tfp = 0; Tfn = 0; decision=zeros(size(data_labels)); prediction=zeros(size(data_labels));
if isempty(Subsets)
[tra, tes]=GenerateSubsets('nFold',[],data_labels_patlevel,shuffle,n);
else
tra = Subsets{j}.training;
tes = Subsets{j}.testing;
end
for i=1:n
fprintf(['Fold # ' num2str(i) '\n']);
training_idx = ismember(patient_labels, unique_patient_labels(tra{i}));
testing_idx = ismember(patient_labels, unique_patient_labels(tes{i}));
training_set = data_set(training_idx,:);
testing_set = data_set(testing_idx,:);
training_labels = uint8(data_labels(training_idx));
testing_labels = uint8(data_labels(testing_idx));
%
% training_set = data_set(tra{i},:);
% testing_set = data_set(tes{i},:);
% training_labels = data_labels(tra{i});
% testing_labels = data_labels(tes{i});
%%% do feature selection on the fly
%% using mrmr
if strcmp(para.featureranking,'mrmr')
% map the data in [0,100] and all integers
dataw_discrete=makeDataDiscrete(training_set);
setAll=1:size(training_set,2);
[idx_TTest] = mrmr_mid_d(dataw_discrete(:,setAll), training_labels, para.num_top_feature);
end
if strcmp(para.featureranking,'ttest') | strcmp(para.featureranking,'wilcoxon')
%% using ttest
if strcmp(para.featureranking,'ttest')
[TTidx,confidence] = prunefeatures_new(training_set, training_labels, 'ttestp');
idx_TTest=TTidx(confidence<0.05);
if isempty(idx_TTest)
idx_TTest=TTidx(1:min(para.num_top_feature*2,size(data_set,2)));
end
end
if strcmp(para.featureranking,'wilcoxon')
[TTidx,confidence] = prunefeatures_new(training_set, training_labels, 'wilcoxon');
idx_TTest=TTidx(confidence<0.3);
if isempty(idx_TTest)
idx_TTest=TTidx(1:min(para.num_top_feature*3,size(data_set,2)));
end
end
%%% lock down top features with low correlation
set_candiF=Lpick_top_n_features_with_pvalue_correlation(training_set,idx_TTest,para.num_top_feature,para.correlation_factor);
set_fff=feature_list(set_candiF)';
idx_TTest=set_candiF;
end
%% test on the testing set
% a=setTopF_TTest{1};b=setTopF_TTest{2};
% strcmp
% interr=intersect(a,b);
if strcmp(para.feature_score_method,'addone')
% add one value on the piceked features
feature_scores(idx_TTest)=feature_scores(idx_TTest)+1;
end
if strcmp(para.feature_score_method,'weighted')
feature_scores(idx_TTest)=feature_scores(idx_TTest)+ linspace( para.num_top_feature ,1, length(idx_TTest))';
end
fprintf('on the fold, %d features are picked\n', length(idx_TTest));
try
if strcmp(para.classifier,'BaggedC45')
[temp_stats,methodstring] = Classify( 'BaggedC45', training_set(:,idx_TTest) , testing_set(:,idx_TTest), training_labels(:), testing_labels(:));
% [temp_stats,methodstring] = Classify( 'BaggedC45_perpatient', training_set(:,idx_TTest) , testing_set(:,idx_TTest), training_labels(:), testing_labels(:));
end
if strcmp(para.classifier,'QDA')
[temp_stats,methodstring] = Classify( 'QDA', training_set(:,idx_TTest) , testing_set(:,idx_TTest), training_labels(:), testing_labels(:));
end
if strcmp(para.classifier,'LDA')
[temp_stats,methodstring] = Classify( 'LDA', training_set(:,idx_TTest) , testing_set(:,idx_TTest), training_labels(:), testing_labels(:));
end
if strcmp(para.classifier,'SVM')
if exist('para.params','var')
params.kernel=para.params.kernel;
params.c_range=para.params.c_range;
params.g_range=para.params.g_range;
params.cvfolds=para.params.cvfolds;
[temp_stats,methodstring] = Classify( 'SVM', training_set(:,idx_TTest) , testing_set(:,idx_TTest), training_labels(:), testing_labels(:),params);
else
[temp_stats,methodstring] = Classify( 'SVM', training_set(:,idx_TTest) , testing_set(:,idx_TTest), training_labels(:), testing_labels(:));
end
temp_stats.decision=temp_stats.predicted_labels;
temp_stats.prediction=temp_stats.prob_estimates(:,1);
end
catch
display('Error while using LDA or QDA, the training data is linear dependent, use Random Forest for this fold instead\n');
[temp_stats,methodstring] = Classify( 'BaggedC45', training_set(:,idx_TTest) , testing_set(:,idx_TTest), training_labels(:), testing_labels(:));
end
Ttp = Ttp + temp_stats.tp;
Ttn = Ttn + temp_stats.tn;
Tfp = Tfp + temp_stats.fp;
Tfn = Tfn + temp_stats.fn;
decision(testing_idx) = temp_stats.decision;
% decision(tes{i}) = temp_stats.prediction >= temp_stats.threshold;
prediction(testing_idx) = temp_stats.prediction;
end
decision(decision==0) = -1;
%% for patch level performance
% output statistics
if numel(unique(testing_labels))>1 %numel(unique(testing_labels))>1
if n == 1
[FPR,TPR,T,AUC,OPTROCPT,~,~] = perfcurve(data_labels(tes{i}),prediction(tes{i}),1);
else
[FPR,TPR,T,AUC,OPTROCPT,~,~] = perfcurve(data_labels,prediction,1);
end
stats(j).AUC = AUC;
stats(j).TPR = TPR;
stats(j).FPR = FPR;
% stats(j).AUC = [];
% stats(j).TPR = [];
% stats(j).FPR = [];
else
stats(j).AUC = [];
stats(j).TPR = [];
stats(j).FPR = [];
end
stats(j).tp = Ttp;
stats(j).tn = Ttn;
stats(j).fp = Tfp;
stats(j).fn = Tfn;
stats(j).acc = (Ttp+Ttn)/(Ttp+Ttn+Tfp+Tfn);
stats(j).ppv = Ttp/(Ttp+Tfp);
stats(j).sens = Ttp/(Ttp+Tfn);
stats(j).spec = Ttn/(Tfp+Ttn);
stats(j).subsets.training = tra;
stats(j).subsets.testing = tes;
stats(j).labels = data_labels;
stats(j).decision = decision;
stats(j).prediction = prediction;
Pre = ((Ttp+Tfp)*(Ttp+Tfn) + (Ttn+Tfn)*(Ttn+Tfp)) / (Ttp+Ttn+Tfp+Tfn)^2;
stats(j).kappa = (stats(j).acc - Pre) / (1 - Pre);
%% for patient level performance
% output statistics
% data_labels patient_labels
% prediction
% [unique_patient_labels, idx, ~] = unique(patient_labels,'first');
% data_labels_patlevel = data_labels(idx);
for ip=1:length(unique_patient_labels)
cur=unique_patient_labels(ip);
prediction_patlevel(ip)=mean(prediction(patient_labels==cur));
% data_labels_patlevel=data_labels_patlevel
end
if numel(unique(testing_labels))>1 %numel(unique(testing_labels))>1
if n == 1
[FPR,TPR,T,AUC,OPTROCPT,~,~] = perfcurve(data_labels_patlevel(tes{i}),prediction_patlevel(tes{i}),1);
else
[FPR,TPR,T,AUC,OPTROCPT,~,~] = perfcurve(data_labels_patlevel,prediction_patlevel,1);
end
stats_patlevel(j).AUC = AUC;
stats_patlevel(j).TPR = TPR;
stats_patlevel(j).FPR = FPR;
% stats(j).AUC = [];
% stats(j).TPR = [];
% stats(j).FPR = [];
else
stats_patlevel(j).AUC = [];
stats_patlevel(j).TPR = [];
stats_patlevel(j).FPR = [];
end
% not finished below
% stats(j).tp = Ttp;
% stats(j).tn = Ttn;
% stats(j).fp = Tfp;
% stats(j).fn = Tfn;
% stats(j).acc = (Ttp+Ttn)/(Ttp+Ttn+Tfp+Tfn);
% stats(j).ppv = Ttp/(Ttp+Tfp);
% stats(j).sens = Ttp/(Ttp+Tfn);
% stats(j).spec = Ttn/(Tfp+Ttn);
% stats(j).subsets.training = tra;
% stats(j).subsets.testing = tes;
% stats(j).labels = data_labels;
% stats(j).decision = decision;
% stats(j).prediction = prediction;
% Pre = ((Ttp+Tfp)*(Ttp+Tfn) + (Ttn+Tfn)*(Ttn+Tfp)) / (Ttp+Ttn+Tfp+Tfn)^2;
% stats(j).kappa = (stats(j).acc - Pre) / (1 - Pre);
end