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

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

问题描述

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

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

谢谢!

推荐答案

经过处理,我已经能够获得权重.为了获得权重,必须首先获得支持向量,然后将它们乘以 alpha 值相加.

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天全站免登陆