要获得线性模型决策边界线的方程,您需要同时获得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