获取 SVM 的决策边界

Obtain Decision Boundary for SVM

在下面的例子中: http://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane.html

我想得到图中所示的(线)决策边界的系数。 致电

clf.coef_ 

returns

[[-0.2539717  -0.83806387]]

如果我没记错的话,它代表等式

y = -0.83806387 * x - 0.2539717

但是上面的直线不是例子中得到的决策边界,那么coef_到底是什么,如何得到线性决策边界的方程?

如果你想绘制线性图,即 y = ax + b 那么你可以使用下面的代码块

tmp = clf.coef_[0]
a = - tmp[0] / tmp[1]

b = - (clf.intercept_[0]) / tmp[1]

xx = np.linspace(xlim[0], xlim[1])
yy = a * xx + b
plt.plot(xx, yy)

希望对您有所帮助!

要获得线性模型决策边界线的方程,您需要同时获得 coef_intercept_。另请注意,由于您使用的是 SVC,因此将涉及多个决策边界。

线方程可以构造为:

y = w0 + w1 * x1 + w2 * x2 + ...

其中 w0 是从 intercept_ 获得的,w1 以后是在 coef_ 中找到的,x1 以后是你的特征。

例如,此代码向您展示了如何打印出每个决策边界的方程式。

from sklearn import svm
import numpy as np

clf = svm.SVC(kernel="linear")

X = np.array([[1, 2], [3, 4], [5, 1], [6, 2]])
y = np.array(["A", "B", "A", "C"])

clf.fit(X, y)

for (intercept, coef) in zip(clf.intercept_, clf.coef_):
    s = "y = {0:.3f}".format(intercept)
    for (i, c) in enumerate(coef):
        s += " + {0:.3f} * x{1}".format(c, i)

    print(s)

在此示例中,行确定为:

y = 2.800 + -0.200 * x0 + -0.800 * x1
y = 7.000 + -1.000 * x0 + -1.000 * x1
y = 1.154 + -0.462 * x0 + 0.308 * x1

来源:http://scikit-learn.org/stable/modules/linear_model.html

感谢@Prem 和@Christopher Wells - 这真的很有帮助。我合并了这两个答案,因为在 Prem 的代码中没有包含 y_target(--> 参见 Christopher 的回答),它适用于 y_target=0.

作为示例,我使用了 logRegression tutorial from scikitLearn。我插入了以下代码:

mf = logreg.intercept_.shape[0];
xm = np.r_[np.min(X),np.max(X)]
yf = logreg.classes_.copy()
xm = np.r_[np.min(X),np.max(X)]

for jf in np.arange(mf):
    tmp = logreg.coef_[jf]
    a = - tmp[0] / tmp[1]; 
    b = - (logreg.intercept_[jf]-yf[jf]) / tmp[1]
    yy = a * xm + b
    plt.plot(xm, yy, label='Coeff ='+str(jf))
plt.legend()
plt.show()

这非常适合 y_target=0(参见 skLearn 示例中的图形)。但是其他两条直线是什么?还有什么需要考虑的吗?