Extract coefficients/weights from libsvm model fil

2019-09-14 02:55发布

问题:

I am using libsvm for creating a 2-classes classifier. I wish to extract the coefficient/weight of each feature used by the model generated by ./svm-train training.training model.model

The model.model file looks like:

svm_type c_svc
kernel_type rbf
gamma 8
nr_class 2
total_sv 442
rho 21
label 1 -1
nr_sv 188 254
SV
7080.357768871263 0:0 1:0.00643 2:0.01046 3:0.00963 4:0.02777 5:0.04338 19:0.04468 
528.7111702760092 0:0 1:0.00058 3:0.00086 6:0.01158 7:0.0028 9:0.08991 13:0.0096 
...
391.7649705739246 0:0 1:0.00055 3:0.00082 5:0.04615 7:0.06374 21:0.00374 31:0.00339 33:0.00395 38:0.16343 
...
-564.1329424321915 0:0 1:0.00709 2:0.00384 3:0.00709 5:0.00399 9:0.01457 10:0.01244 11:0.0206 17:0.02124 20:0.00565 23:0.00846 27:0.04692 33:0.04271 35:0.02389 36:0.00859 39:0.02014 

How do I know which coefficients/weights will be used by svm-predict [options] test.test model.model out.out ? The ones from the last line ?

Thanks, M.

回答1:

The model file generated by LIBSVM consists, according to the offical FAQ entry, of the following information:

In the model file, after parameters and other informations such as labels , each line represents a support vector. Support vectors are listed in the order of "labels" shown earlier. (i.e., those from the first class in the "labels" list are grouped first, and so on.) If k is the total number of classes, in front of a support vector in class j, there are k-1 coefficients y*alpha where alpha are dual solution of the following two class problems: 1 vs j, 2 vs j, ..., j-1 vs j, j vs j+1, j vs j+2, ..., j vs k and y=1 in first j-1 coefficients, y=-1 in the remaining k-j coefficients. For example, if there are 4 classes, the file looks like:

+-+-+-+--------------------+
|1|1|1|                    |
|v|v|v|  SVs from class 1  |
|2|3|4|                    |
+-+-+-+--------------------+
|1|2|2|                    |
|v|v|v|  SVs from class 2  |
|2|3|4|                    |
+-+-+-+--------------------+
|1|2|3|                    |
|v|v|v|  SVs from class 3  |
|3|3|4|                    |
+-+-+-+--------------------+
|1|2|3|                    |
|v|v|v|  SVs from class 4  |
|4|4|4|                    |
+-+-+-+--------------------+

There is also an example of how to read this data in order to compute w for a binary classifier.