【问题标题】:Decision Boundary Plot for Support Vector Classifier (distance from separating hyperplane)支持向量分类器的决策边界图(与分离超平面的距离)
【发布时间】:2019-01-17 14:59:15
【问题描述】:

我正在阅读 Aurélien Géron 所著的“使用 Scikit-Learn 和 TensorFlow 进行动手机器学习”一书。下面的代码是用 Python 3 编写的。

在章节的 GitHub 页面上。支持向量机问题的 5 种解决方案有以下代码用于绘制 SVC 决策边界 (https://github.com/ageron/handson-ml/blob/master/05_support_vector_machines.ipynb):

def plot_svc_decision_boundary(svm_clf, xmin, xmax):
    w = svm_clf.coef_[0]
    b = svm_clf.intercept_[0]

    # At the decision boundary, w0*x0 + w1*x1 + b = 0
    # => x1 = -w0/w1 * x0 - b/w1
    x0 = np.linspace(xmin, xmax, 200)
    decision_boundary = -w[0]/w[1] * x0 - b/w[1]

    margin = 1/w[1]
    gutter_up = decision_boundary + margin
    gutter_down = decision_boundary - margin

    svs = svm_clf.support_vectors_
    plt.scatter(svs[:, 0], svs[:, 1], s=180, facecolors='#FFAAAA')
    plt.plot(x0, decision_boundary, "k-", linewidth=2)
    plt.plot(x0, gutter_up, "k--", linewidth=2)
    plt.plot(x0, gutter_down, "k--", linewidth=2)

我的问题是为什么边距定义为1/w[1]?我相信边距应该是1/sqrt(w[0]^2+w[1]^2)。也就是说,边距是2/L_2_norm(weight_vector) 的一半,即1/L_2_norm(weight_vector)。见https://math.stackexchange.com/questions/1305925/why-does-the-svm-margin-is-frac2-mathbfw

这是代码中的错误吗?

【问题讨论】:

    标签: python-3.x machine-learning scikit-learn svm


    【解决方案1】:

    给定:

    决策边界:w0*x0 + w1*x1 + b = 0

    gutter_up: w0*x0 + w1*x1 + b = 1,即w0*x0 + w1*(x1 - 1/w1) + b = 0

    gutter_down:w0*x0 + w1*x1 + b = -1,即w0*x0 + w1*(x1 + 1/w1) + b = 0

    对应决策边界线中的(x0,x1),(x0,x1+1/w1)和(x0,x1-1/w1)是点在 gutter_up/down 行中。

    【讨论】:

    • 好的,我想我现在明白了。但是,似乎将 1/w1 称为边距是用词不当,因为 1/w1 不是决策边界和排水沟之间的正交“距离”。但是 1/w1 仅仅是我们必须移动 x1 而 x0 固定以将点 (x0, x1) 移动到我们的排水沟线之一的量。这是正确的吗?
    • 绝对距离不是很有用,因为将w0, w1, b缩放相同的数量,你可以得到任何你想要的,但Separating Hyperplane保持不变
    • 为什么我们将它设置为零?
    猜你喜欢
    • 2020-05-10
    • 2010-12-04
    • 2013-01-28
    • 2020-07-12
    • 2015-11-11
    • 2012-10-31
    • 2019-12-09
    • 2022-01-10
    • 2020-11-22
    相关资源
    最近更新 更多