在CvSVM中获取权重,OpenCV的SVM实现 [英] Obtaining weights in CvSVM, the SVM implementation of OpenCV

查看:689
本文介绍了在CvSVM中获取权重,OpenCV的SVM实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS上使用OpenCV的SVM实现(基于LibSVM)。是否可以获得训练后的权重向量?

I am using the SVM implementation of OpenCV (based on LibSVM) on iOS. Is it possible to obtain the weight vector after training?

谢谢!

推荐答案

能够获得重量。为了获得权重,必须首先获得支持向量,然后将它们与α值相乘。

After dealing with it I have been able to obtain the weights. For obtaining the weights one has to obtain first the support vectors and then add them multiplied by the alpha values.

// get the svm weights by multiplying the support vectors by the alpha values
int numSupportVectors = SVM.get_support_vector_count();
const float *supportVector;
const CvSVMDecisionFunc *dec = SVM.decision_func;
svmWeights = (float *) calloc((numOfFeatures+1),sizeof(float));
for (int i = 0; i < numSupportVectors; ++i)
{
    float alpha = *(dec[0].alpha + i);
    supportVector = SVM.get_support_vector(i);
    for(int j=0;j<numOfFeatures;j++)
        *(svmWeights + j) += alpha * *(supportVector+j);
}
*(svmWeights + numOfFeatures) = - dec[0].rho; //Be careful with the sign of the bias!

这里唯一的窍门是实例变量 float * decision_function 在opencv框架上受保护,所以我必须更改它才能访问它。

The only trick here is that the instance variable float *decision_function is protected on the opencv framework, so I had to change it in order to access it.

这篇关于在CvSVM中获取权重,OpenCV的SVM实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆