arn*_*107 3 python machine-learning svm scikit-learn
在以下示例中: http://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane.html
我想获得图中所示的(线)决策边界的系数。致电
clf.coef_
Run Code Online (Sandbox Code Playgroud)
回报
[[-0.2539717 -0.83806387]]
Run Code Online (Sandbox Code Playgroud)
如果我没记错的话,它代表了方程组
y = -0.83806387 * x - 0.2539717
Run Code Online (Sandbox Code Playgroud)
然而,上面的直线并不是例子中得到的决策边界,那么 coef_ 到底是什么,如何得到线性决策边界的方程呢?
要获得线性模型决策边界线的方程,您需要同时获得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)
Run Code Online (Sandbox Code Playgroud)
在此示例中,行确定为:
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
Run Code Online (Sandbox Code Playgroud)
资料来源:http ://scikit-learn.org/stable/modules/linear_model.html