-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestkn.m
More file actions
41 lines (32 loc) · 785 Bytes
/
testkn.m
File metadata and controls
41 lines (32 loc) · 785 Bytes
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
function [acc sen spec]=testkn(X,Y)
k=cvpartition(126,'kfold',10);
acc=[];
spec=[];
sen=[];
for i=1:10
mdl=fitcknn(X(k.training(i),:),Y(k.training(i)),'NumNeighbors',7);
mdlres = mdl.predict(X(k.test(i),:));
tru = Y(k.test(i));
acci = mdlres==tru;
acci =find(acci==1);
acci = size(acci,1)/size(mdlres,1);
acc=[acc;acci];
P = find(mdlres==1);
P = size(P,1);
N = find(mdlres==2);
N = size(N,1);
TP = (mdlres==tru.* (tru==1));
TN = (mdlres==tru.* (tru==2));
TP = find(TP==1);
TP = size(TP,1);
TN = find(TN==1);
TN = size(TN,1);
seni = TP/P;
speci =TN/N;
spec=[spec;speci];
sen=[sen;seni];
end
acc=mean(acc);
spec=mean(spec);
sen=mean(sen);
end