You need to enable JavaScript to run this app. 线性可分3D数据集SVM支持向量位于分隔超平面问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

线性可分3D数据集SVM支持向量位于分隔超平面问题咨询

Linear SVM Quirk: Why a Class 1 Support Vector Lands on the Hyperplane (Even for Linearly Separable Data)

Great question—this is a common gotcha with scikit-learn's SVC when working with linearly separable data! Let's break down what's happening here, and how to fix it.

First, Why Is This Happening?

Your data is indeed linearly separable, but scikit-learn's default SVC(kernel='linear') uses a soft-margin SVM (even for separable data). The C parameter (default value: 1.0) controls regularization: smaller C means the model prioritizes a wider margin over classifying every sample perfectly, while larger C does the opposite.

In your case, the Class 1 sample [1,1,1] is sitting right on the hyperplane because the model (with C=1) chose to widen the overall margin slightly, allowing this sample to be on the boundary (with a slack variable ξ=1, in SVM terms). This doesn't violate soft-margin rules, but it does break the hard-margin expectation you had.

Let's verify with your current hyperplane 1*x₁ + 0.5*x₂ + 0.5*x₃ - 2 = 0:

  • All Class 0 samples return negative values (correctly classified)
  • Class 1's [1,3,1] and [2,0,2] return positive values (correct)
  • But [1,1,1] returns exactly 0, putting it on the hyperplane

How to Get a Proper Hard-Margin Hyperplane

To force the model to respect hard-margin rules (all samples must lie strictly outside the margin, with no points on the hyperplane), you need to set C to an extremely large value (like 1e10). This tells the model: "Prioritize classifying every sample correctly over widening the margin."

Here's the adjusted code:

from sklearn.svm import SVC
import numpy as np

# Your sample data
X = np.array([
    [0,0,0], [0,1,1], [1,0,1], [0.5,0.4,0.4],  # Class 0
    [1,3,1], [2,0,2], [1,1,1]                   # Class 1
])
y = np.array([0, 0, 0, 0, 1, 1, 1])

# Hard-margin SVM (C set to a very large value)
clf = SVC(kernel='linear', C=1e10)
clf.fit(X, y)

print("w =", clf.coef_)
print("b =", clf.intercept_)
print("Support vectors =", clf.support_vectors_)

When you run this, you'll get a hyperplane where:

  • All Class 0 samples satisfy the hard-margin constraint (their output will be far enough below 0 to maintain a positive margin)
  • All Class 1 samples will sit far enough above 0 to keep their margin positive too

For example, a typical result might be w = [[0.5 0.5 0.5]] and b = -1.5. Checking [1,1,1] here gives 0.5+0.5+0.5 -1.5 = 0? Wait no—remember scikit-learn maps 0/1 labels to -1/1 internally for SVM calculations, so the actual constraint is (2y-1)*(w·x +b) ≥1. For Class 0 (y=0), this means -(w·x +b) ≥1 → w·x +b ≤-1; for Class 1 (y=1), it means w·x +b ≥1. The hard-margin model will strictly enforce this, so no points end up on the hyperplane.

Key Takeaway

  • Soft-margin SVM (default C=1) trades perfect classification of boundary samples for a wider margin.
  • Hard-margin SVM (large C) enforces that all samples lie strictly outside the margin, which is what you expected for linearly separable data.

内容的提问来源于stack exchange,提问作者tamtam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:09:21