1、通过官方网站修改 svm.cpp 来计算:
---------------------------------------------分 割 线-----------------------------------------------------
首先,查官方文档--> LIBSVM FAQ
获得以下信息:
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 the place where we calculate the dual objective value (i.e., the subroutine Solve()) and add a statement to print w^Tw.
More precisely, here is what you need to do :
a) Search for "calculate objective value" in svm.cpp
b) In that place, si->obj is the variable for the objective value
c) Add a for loop to calculate the sum of alpha
d) Calculate 2*(si->obj + sum of alpha) and print the square root of it. You now get |w|. You need to recompile the code
e) Check an earlier FAQ on printing decision values. You need to recompile the code
f) Then print decision value divided by the |w| value obtained earlier.
可知两个公式:
|w|^2=2*(dual_obj + sum alpha_i).
distance = |decision_value| / |w|.
以下为过程记录:【温馨提示:修改svm.cpp文件前,请做好此文件备份^_^】
1)在svm.cpp(路径参考C:\Program Files\MATLAB\R2014a\toolbox\libsvm-3.21)内搜索calculate objective value
si->obj 即为 the variable for the objective value
2)用for循环求出alpha值之和sum_alpha:
3) 计算2*(si->obj + sum of alpha) 并打印。
// calculate objective value
{
double sum_alpha = 0,w = 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;
w = 2*(si->obj + sum_alpha);
w = sqrt(w);
FILE * fp = fopen("w_values.txt","a+");
fprintf(fp,"w_values %f\n",w);
}
【头文件math.h看有没有没有就加上,我也忘记我是不是手动加的了。。。
【头文件math.h看有没有没有就加上,我也忘记我是不是手动加的了。。。
现在svm.cpp代码修改好了,我们需要重新编译一下了。
路径参考:C:\Program Files\MATLAB\R2014a\toolbox\libsvm-3.21\matlab
在matlab下打开以上路径,然后make一下,生成的那四个mexw64文件就是新的
4)decison_value不用说了吧,predict的时候会生成的
5)公式里缺失的值现在都得到了。之后就是根据公式计算了~
2、网络方法二
最近在看的资料里涉及到计算 点到支持向量机分类超平面的距离 这一点内容,我使用的svm是libsvm。
由于是新手,虽然看了一些资料,但中英转换误差等等原因导致经常出现理解错误,因此对libsvm的了解是磕磕绊绊。在摸索libsvm各种返回值的意义和运用它产生的结果文件过程中绕了不少弯。
最开始接触这个问题的解答是在libsvm本身的faq中:
Q: How do I get the distance between a point and the hyperplane?
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 the place where we calculate the dual objective value (i.e., the subroutine Solve()) and add a statement to print w^Tw.
这里Q不知道是什么东西,不过至少可以知道
distance = |decision_value|/|w| = |decision_value|/sqr(2*(dual_obj+sum(αi))
但是,decision_value到底是神马!dual_obj和sum(αi)到底在哪儿!!初学者博主很想咆哮有木有!!!
那我们先来看看从libsvm的返回值和结果文件中都能得到什么东西。
首先,在训练模型的时候(就是用svm_train()这个函数的时候),能在终端得到这些返回值(我写python程序都是在终端运行):
#iter:迭代次数
nu:选择的核函数类型的参数
obj:svm文件转换成的二次规划求解得到的最小值(这里混入了奇怪的东西是不是,没错!隐藏的这么深的就是obj,看看上面的距离公式)
rho:决策函数的偏置项b(决策函数f(x)=w^T*x+b)
nSV:标准支持向量的个数(0<αi<c)
nBSV:边界上的支持向量的个数(αi=c)
Total nSV:支持向量的总个数(二分类的话就等于nSV,多分类的话就是多个分界面上nSV的和)
我才不会告诉你刚才这些返回值的解读是来自这里:http://blog.163.com/shuangchenyue_8/blog/static/399543662010328101618513/
接下来再看训练模型所生成的model文件,其中的信息包括:
svm_type c_svc (svm的种类,取的默认值)
kernel_type rbf (核函数的类型,这里取的默认值)
gamma 0.0117647 (参数γ)
nr_class 2 (几分类,我这里做的是2分类)
total_sv 1684
rho -0.956377
label 0 1 (两种类别的标识)
nr_sv 1338 346 (所有支持向量中两种类别各自占了多少)
SV (哦哦,以下就是真正的支持向量啦,看看长什么样)
0.536449657223129 1:39 2:2 3:3 。。。。
0.3766245470441405 1:11 3:3 4:3 。。。。
数据格式都是 前面一个数+空格+向量
前面这个数是什么捏?它就是我们苦苦寻找的α啊!! 后面的向量不用多说,就是支持向量本尊喽,这里跟训练数据不同的是它的存储格式,稀疏向量存储的方式,把值为0的剔掉不记录,比如这里
1:11 3:3 4:3。。。。
其实是 1:11 2:0 3:3 4:3。。。。
到现在为止我们的obj和α都出现啦,现在就剩decision_value啦。其实这个时候我们就可以算出分类超平面的值了:
f(x)=w^T*x+b
w=∑αi*yi*αi 后面这个绿色标注的代表向量(这里的表达式参见这位写的svm资料,不错哦http://blog.csdn.net/v_july_v/article/details/7624837),这样α知道了,b知道了(就是上面的rho),分类超平面方程就得到了,根据空间中点到面的距离d=|f(x)|/|w|,把要考察的点x带入,是不是!!计算出来啦!!!
不过有时候想偷懒,不想自己计算|f(x)|怎么办?
没关系!libsvm在手,天下我有!(原谅博主疯疯癫癫,快半夜了有点兴奋)
来看看svm_predict()这个函数给我们返回了什么信息:
一般不是这样写的嘛 p_labels,p_acc,p_vals=svm_predict(......)
p_labels就是模型对这些测试数据预测的label
p_acc就是分类准确率啦,均方差啦还有squared correlation coefficient(相关系数平方?)
p_vals:在libsvm python interface 中这样说,a list of decision values or probability estimates(if '-b 1'is specified)这不就是我们要找的决策值吗!!%>_<%