Distance between a point and the hyperplane in SVM, MATLAB

孤者浪人 提交于 2019-12-14 02:35:20

问题


I'm using LIBSVM for MATLAB. Now I'm trying to compute the the distance between a point and the hyperplane.

According to the official solution (http://www.csie.ntu.edu.tw/~cjlin/libsvm/faq.html#f4151):

"The distance is

|decision_value| / |w|

We have

|w|^2 = w^Tw = alpha^T Q alpha = 2*(dual_obj + sum alpha_i). 

Thus in svm.cpp please find theplace where we calculate the dual objective value (i.e., the subroutine Solve()) and add a statement to print w^Tw."

But I'm a little confused: there are over 3,000 lines in 'svm.cpp', where is the 'place where we calculate the dual objective'? Could anyone tell me how to make it???

Or is there any other solution to figure out the distance from hyperplane?

Thank you!


回答1:


Disclaimer: This is not an answer. I am not familiar with this package at all, so I am just speculating. I would have put this in the comments, but it wouldn't fit, so here it is.

I think that the code you are looking for might be around line #747 of svm.cpp where the objective value is computed. Note that the FAQ references the dual objective value, but the word "dual" does not appear in svm.cpp. So if we assume that the function is set up to solve the dual problem from the beginning then this might work:

// calculate objective value
{
    char buf[BUFSIZ];
    double sum_alpha = 0;

    double v = 0;
    int i;
    for(i=0;i<l;i++)
    {
        v += alpha[i] * (G[i] + p[i]);
        sum_alpha += alpha[i];
    }
    si->obj = v/2;

    vsprintf(buf,"Distance to hyperplane = %f",v+2*sum_alpha);
    (*svm_print_string)(buf);
}

I have not compiled nor tested this code, neither do I know if this is the right thing to do, so take this entire answer with a big grain of salt



来源:https://stackoverflow.com/questions/19450609/distance-between-a-point-and-the-hyperplane-in-svm-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!