forked from gaobb/CDFTSVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftsvmclass.m
More file actions
75 lines (61 loc) · 2.2 KB
/
ftsvmclass.m
File metadata and controls
75 lines (61 loc) · 2.2 KB
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
function [acc,outclass,time, fp, fn]= ftsvmclass(ftsvm_struct,Testdata,Testlabel)
% Function: testing ftsvm on test data
% Input:
% ftsvm_struct - the trained ftsvm model
% Testdata - test data
% Testlabel - test label
%
% Output:
% acc - accuracy
% outclass - predict label
%
% Author: Bin-Bin Gao (csgaobb@gmail.com)
% Created on 2014.10.10
% Last modified on 2017.04.14
% Last modified on 2015.07.16
if ( nargin>3||nargin<2) % check correct number of arguments
help ftsvmclass
else
[rt,ct]=size(Testdata);
tic;
if ~isempty(ftsvm_struct.scaleData)
scaleData=ftsvm_struct.scaleData;
for k = 1:size(Testdata, 2)
Testdata(:,k) = scaleData.scaleFactor(k) * ...
(Testdata(:,k) + scaleData.shift(k));
end
end
groupString=ftsvm_struct.groupString;
vp=ftsvm_struct.vp;
vn=ftsvm_struct.vn;
X=ftsvm_struct.X;
kfun =ftsvm_struct.KernelFunction;
kfunargs = ftsvm_struct.KernelFunctionArgs;
fprintf('Testing ...\n');
switch ftsvm_struct.Parameter.ker
case 'linear'
fp=(Testdata*vp(1:(length(vp)-1))+vp(length(vp)))./norm(vp(1:(length(vp)-1)));
fn=(Testdata*vn(1:(length(vn)-1))+vn(length(vn)))./norm(vn(1:(length(vn)-1)));
case 'rbf'
K = feval(kfun,Testdata,X,kfunargs{:});
fp=(K*vp(1:(length(vp)-1))+vp(length(vp)))./norm(vp(1:(length(vp)-1)));
fn=(K*vn(1:(length(vn)-1))+vn(length(vn)))./norm(vn(1:(length(vn)-1)));
end
f=fp+fn;
classified=ones(rt,1);
classified(abs(fp)>abs(fn)) = -1;
classified(classified == -1) = 2;
outclass = classified;
unClassified = isnan(outclass);
[~,groupString,glevels] = grp2idx(ftsvm_struct.L);
outclass = glevels(outclass(~unClassified),:);
if nargin==3
correct=sum(outclass==Testlabel);
acc=100*correct/length(Testlabel);
fprintf('Accuracy : %3.4f (%d/%d)\n',acc,correct,length(Testlabel));
else
acc=[];
fprintf('the accuracy can not be calculated, because of lack of the labels of testing data\n');
end
time= toc;
end