简体   繁体   English

如何获取scikit-learn SVM分类器的所有alpha值?

[英]How to get all alpha values of scikit-learn SVM classifier?

I need the alpha values, which are the Lagrange multipliers of the SVM dual problem, after training a SVM classifier with scikit-learn. 在使用scikit-learn训练SVM分类器之后,我需要alpha值,这是SVM双重问题的拉格朗日乘数。 According to the document, it seems that scikit-learn provides only svm.dual_coef_ , which is the product of the Lagrange multiplier alpha and the label of a data point. 根据该文档,似乎scikit-learn仅提供svm.dual_coef_ ,它是拉格朗日乘数α和数据点标签的乘积。

I tried to calculate the alpha value manually by dividing the elements of svm.dual_coef_ by the data label, but since svm.dual_coef_ stores only the coefficients of the support vectors, I'm not sure if I iterate over this array, the order of support vectors would be the same as the order in the original training data. 我试图通过将svm.dual_coef_的元素除以数据标签手动计算alpha值,但由于svm.dual_coef_只存储支持向量的系数,我不确定是否迭代这个数组,顺序为支持向量与原始训练数据中的顺序相同。

So is there a reliable way to get the alpha values of support vectors? 那么有一种可靠的方法来获得支持向量的alpha值吗?

As alpha values are by definition positive you can get it through taking abs of dual_coefs: 由于alpha值的定义是正面的,你可以通过abs的dual_coefs得到它:

alphas = np.abs(svm.dual_coef_)

whis is a direct consequence of the fact that 威尔是事实的直接后果

svm.dual_coef_[i] = labels[i] * alphas[i]

where labels[i] is either -1 or +1 and alphas[i] are always positive. 其中labels[i]-1+1alphas[i]总是正数。 Futhermore, you can also get each label through 此外,您还可以获得每个标签

labels = np.sign(svm.dual_coef_)

using the same observation. 使用相同的观察。 This is also why scikit-learn does not store alphas as such - they are uniquely represented by dual_coefs_, together with labels. 这也是为什么scikit-learn不存储alphas的原因 - 它们由dual_coefs_以及标签唯一地表示。

It is easy to understand it once you analyze all possible cases: 一旦分析了所有可能的情况,就很容易理解它:

  • labels[i] == -1 and alphas[i] > 0 => dual_coef_[i] < 0 and dual_coef_[i] == -alphas[i] == labels[i] * alphas[i] labels[i] == -1 and alphas[i] > 0 => dual_coef_[i] < 0 and dual_coef_[i] == -alphas[i] == labels[i] * alphas[i]
  • labels[i] == -1 and alphas[i] < 0 => impossible (alphas are non-negative) labels[i] == -1alphas[i] < 0 => 不可能(alphas为非负数)
  • labels[i] == -1 and alphas[i]== 0 => it is not a support vector labels[i] == -1alphas[i]== 0 => 它不是支持向量
  • labels[i] == +1 and alphas[i] > 0 => dual_coef_[i] > 0 and dual_coef_[i] == alphas[i] == labels[i] * alphas[i] labels[i] == +1alphas[i] > 0 => dual_coef_[i] > 0dual_coef_[i] == alphas[i] == labels[i] * alphas[i]
  • labels[i] == +1 and alphas[i] < 0 => impossible (alphas are non-negative) labels[i] == +1alphas[i] < 0 => 不可能(alphas是非负的)
  • labels[i] == +1 and alphas[i]== 0 => it is not a support vector labels[i] == +1alphas[i]== 0 => 它不是支持向量

Consequently, if dual_coef_[i] is positive then it is the alphas[i] coefficient, and it belongs to positive class, and if it is negative, alphas[i] == -dual_coef_[i] and it belongs to negative class. 因此,如果dual_coef_[i]为正,则它是alphas[i]系数,并且它属于正类,如果它是负数,则alphas[i] == -dual_coef_[i]并且它属于负类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM