# libsvm源代码注释+算法描述:svm_predict

(I will try my best to make this note clearer. We mainly discuss one_class case while others will be introduced in the future.)

Starting from the usage of svm-predict:
./svm-predict svmguide1.t svmguide1.model svmguide1.t.predict

This is copied from the instruction of libsvm.

The model is loaded from svmguide1.model, which is output by training before.

Later, the prediction will be written in the file: svmguide1.t.predict.

the MAIN functions are:

predict(input,output);
svm_predict(model, x);  //x data is used for prediction
svm_predict_values(model, x, dec_values);
//dec_values is one for one_class, or nr_class*(nr_class-1)/2

For function: svm_predict_values(const svm_model model, const svm_node *x, double dec_values):

    double *sv_coef = model->sv_coef;
double sum = 0;
for(i=0;i<model->l;i++)
sum += sv_coef[i] * Kernel::k_function(x,model->SV[i],model->param);
sum -= model->rho;
*dec_values = sum;
if(model->param.svm_type == ONE_CLASS)
return (sum>0)?1:-1;
else
return sum;

sv_coef is the first column of model file, which is yiα$y_i * \alpha$. (coefficients for SVs in decision functions (sv_coef[k-1][l]) )
And the decision function is: sgn(i=1lyiαiK(xi,x)ρ)$sgn(\sum\limits_{i=1}^{l}y_i \alpha_i K(x_i,x) - \rho)$, ρ=b$\rho =-b$.
Kernel::k_function(x,model->SV[i],model->param): “x” and “SV[i]” are vectors here;
For function k_function(const svm_node *x, const svm_node *y, const svm_parameter& param), only linear and poly are shown here:

    case LINEAR:
return dot(x,y);
case POLY:
return powi(param.gamma*dot(x,y)+param.coef0,param.degree);

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 