【问题标题】:Obtain Decision Boundary for SVM获取 SVM 的决策边界
【发布时间】:2018-02-04 07:13:48
【问题描述】:

在以下示例中: http://scikit-learn.org/stable/auto_examples/svm/plot_separating_hyperplane.html

我想获得图中所示(线)决策边界的系数。 打电话给

clf.coef_ 

返回

[[-0.2539717  -0.83806387]]

如果我没记错的话,它代表方程线

y = -0.83806387 * x - 0.2539717

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

【问题讨论】:

    标签: python machine-learning scikit-learn svm


    【解决方案1】:

    感谢@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 示例中的图形)。但是另外两条直线是怎么回事?还有什么需要考虑的吗?

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • Tx 为您解释!是的,这有帮助。有关带有示例的测试,请参阅我的答案..
      【解决方案3】:

      如果你想绘制线性图,即 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)
      

      希望这会有所帮助!

      【讨论】:

      • 是的,这有帮助。有关带有示例的测试,请参阅我的答案。
      猜你喜欢
      • 2013-12-13
      • 2021-07-30
      • 2017-09-20
      • 2019-09-10
      • 2019-09-26
      • 2019-09-07
      • 2019-01-18
      • 2014-07-10
      • 2016-07-13
      相关资源
      最近更新 更多