Strict class labels in SVM
I'm using one-vs-all to do a 21
-class svm categorization.
I want the label -1
to mean "not in this class" and the label 1
to mean "indeed in this class" for each of the 21
kernels.
I've generated my pre-computed kernels and my test vectors using this standard.
Using easy.py
everything went well for 20
of the classes, but for one of them the labels were switched so that all the inputs that should have been labelled with 1
for being in the class were instead labelled -1
and vice-versa.
The difference in that class was that the first vector in the pre-computed kernel was labelled 1
, while 开发者_如何学编程in all the other kernels the first vector was labelled -1
. This suggests that LibSVM relabels all of my vectors.
Is there a way to prevent this or a simple way to work around it?
You already discovered that libsvm uses the label -1 for whatever label it encounters first. The reason is, that it allows arbitrary labels and changes them to -1 and +1 according to the order in which they appear in the label vector.
So you can either check this directly or you look at the model returned by libsvm.
It contains an entry called Label
which is a vector containing the order in which libsvm encountered the labels. You can also use this information to switch the sign of your scores.
If during training libsvm encounters label A first, then during prediction libsvm will use positive values for assigning object the label A and negative values for another label.
So if you use label 1 for positive class and 0 for negative, then to obtain right output values you should do the following trick (Matlab).
%test_data.y contains 0-s and 1-s
[labels,~,values] = svmpredict(test_data.y, test_data.X, model, ' ');
if (model.Label(1) == 0) % we check which label was encountered by libsvm first
values = -values;
end
精彩评论