Reputation: 119
I am using libsvm for a one-class classification problem. I am trying to select the ideal C and gamma parameters for different kernels(polynomial, linear and rbf) I am using the suggested matlab code that finds the the best parameters through a v-fold validation technique.
bestcv = 0;
for log2c = -1:3,
for log2g = -4:1,
cmd = ['-v 5 -c ', num2str(2^log2c), ' -g ', num2str(2^log2g)];
cv = svmtrain(Target_train, train, cmd);
if (cv >= bestcv),
bestcv = cv; bestc = 2^log2c; bestg = 2^log2g;
end
fprintf('%g %g %g (bestc=%g, g=%g, rate=%g)\n', log2c, log2g, cv, bestc, bestg, bestcv);
end
end
In v-fold cross-validation, we first divide the training set into v subsets of equal size. Sequentially one subset is tested using the classifier trained on the remaining v − 1 subsets. Thus, each instance of the whole training set is predicted once so the cross-validation accuracy is the percentage of data which are correctly classified.
In this code, the C and gamma take values in a range of (2^-1, 2^3) and (2^-4, 2^1)
I noticed that when the svmtrain function is called there is no specified parameter for -s which controls the the type of svm. The default parameter for -s in libsvm is 0 which is for C-SVC. I have a one-class clssification problem so I should be using -s 2 according to svmtrain options. However when I modify the 4th line of the above code into
cmd = ['-s 2 -v 5 -c ', num2str(2^log2c), ' -g ', num2str(2^log2g)];
I am getting this error:
Undefined function 'ge' for input arguments of type 'struct'.
Error in ergasia (line 37) if (cv >= bestcv),
For what I know, svm returns a model of type struct. My question is, is the code I am using suitable for parameter selection in a one class classification problem?
An other question: Is there a better way of defining the best C and gamma other than that? I found this method here: http://www.csie.ntu.edu.tw/~cjlin/papers/guide/guide.pdf
I could really use some help with that so thank you in advance.
Upvotes: 0
Views: 2801
Reputation: 11
I just donging something about LibSvm these days .And I also use gridSearch to get the best c,g ,it does work. Fisrt, I run a little test with -s 2 -v 5 -c -g 12
traindata=getData(1,1,12);
testdata=getData(1,0,12);
trainLabel=getLabel(1);
trainLabel(trainLabel>1)=0;
testLabel=getLabel(0);
testLabel(testLabel>1)=0;
v=5;
c=4;
g=12;
cmd = ['-s 2 -t 2 -v ',num2str(v),' -c ',num2str( c ),' -g ',num2str( g ),' -q '];
cg = svmtrain(trainLabel,traindata,cmd);
I get a result cg ,it just a number
>> littletest
Cross Validation Accuracy = 4.53333%
>> cg
cg =
4.5333
'if (cv >= bestcv)' so it will work.
I also want to tell you is that,if you don't specify parameter for -s,it will also work .
traindata=getData(1,1,12);
testdata=getData(1,0,12);
trainLabel=getLabel(1);
trainLabel(trainLabel>1)=0;
testLabel=getLabel(0);
testLabel(testLabel>1)=0;
v=5;
c=4;
g=12;
cmd = [' -t 2 -v ',num2str(v),' -c ',num2str( c ),' -g ',num2str( g ),' -q '];
cg = svmtrain(trainLabel,traindata,cmd);
>> littletest
Cross Validation Accuracy = 99.0667%
>> cg
cg =
99.0667
second ,I paste the gridSearch code I use here ,hope it can be helpful for you .
[X,Y] = meshgrid(cmin:cstep:cmax,gmin:gstep:gmax);
[m,n] = size(X);
cg = zeros(m,n);
%% record acc with different c & g,and find the bestacc with the smallest c
bestc = 0;
bestg = 0;
bestacc = 0;
basenum = 2;
for i = 1:m
for j = 1:n
cmd = ['-t 2 -v ',num2str(v),' -c ',num2str( basenum^X(i,j) ),' -g ',num2str( basenum^Y(i,j) ),' -q '];
cg(i,j) = svmtrain(train_label, train, cmd);
if cg(i,j) > bestacc
bestacc = cg(i,j);
bestc = basenum^X(i,j);
bestg = basenum^Y(i,j);
end
if ( cg(i,j) == bestacc && bestc > basenum^X(i,j) )
bestacc = cg(i,j);
bestc = basenum^X(i,j);
bestg = basenum^Y(i,j);
end
end
end
Upvotes: 1
Reputation: 1
In one class svm there is no gamma parameter. There is cost (-c) and nu (-n). You can see all the options in the svmtrain.c file inside the matlab folder of the libsvm distribution.
Upvotes: -1