要獲得線性模型決策邊界線的方程,您需要同時獲得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