"""用可证明最优的小数据求二维线性硬间隔,再检查留出预测。""" import json import matplotlib.pyplot as plt import numpy as np def hard_margin_example(x, y): # 行是二维样本,布尔索引按标签保留每一类的所有点。 negative, positive = x[y == -1], x[y == 1] # 找到最近的异类样本对。它给任何可行 w 的范数提供下界, # 但只有整批样本都满足间隔约束时,这个候选才证实为最优解。 difference = positive[None, :, :] - negative[:, None, :] distance2 = np.sum(difference**2, axis=2) # 把最小距离的扁平索引转回“负类行、正类行”的坐标。 i, j = np.unravel_index(distance2.argmin(), distance2.shape) if distance2[i, j] <= 1e-15: raise ValueError("Conflicting labels at the same point") # 缩放法向量,让最近对的得分相差 2;再让中点的得分正好为 0。 w = 2 * difference[i, j] / distance2[i, j] b = -w @ ((negative[i] + positive[j]) / 2) if np.min(y * (x @ w + b)) < 1 - 1e-10: raise ValueError("This sample pair does not certify a hard-margin solution") return w, b def run(): # 人工设置最靠近的异类样本,让最优性可以用两条约束手算核对。 x_train = np.array([[0, 0], [0, 3], [1, 1], [3, 1], [4, 0], [4, 3]], dtype=float) y_train = np.array([-1, -1, -1, 1, 1, 1]) x_test = np.array([[1.25, 2], [1.75, 1.5], [2.25, 0.5], [2.75, 2]]) y_test = np.array([-1, -1, 1, 1]) w, b = hard_margin_example(x_train, y_train) # 乘上标签后,两类都用同一约束 y*f(x) >= 1 检查间隔。 train_margin = y_train * (x_train @ w + b) support = np.isclose(train_margin, 1, atol=1e-10, rtol=0) # 留出得分的正负决定类别,恰好落在边界时归为正类。 score = x_test @ w + b predicted = np.where(score >= 0, 1, -1) width = 2 / np.linalg.norm(w) # 等值线 f(x)=0 是边界。f(x)=±1 之间的带宽是几何间隔。 a, d = np.meshgrid(np.linspace(-0.5, 4.5, 90), np.linspace(-0.4, 3.6, 85)) grid_score = w[0] * a + w[1] * d + b fig, ax = plt.subplots(figsize=(5.9, 4.4), layout="constrained") ax.contourf(a, d, grid_score, levels=[-1, 1], colors=["#e9eef4"]) ax.contour(a, d, grid_score, levels=[-1, 0, 1], colors="gray", linestyles=["--", "-", "--"]) for label, color in [(-1, "C0"), (1, "C1")]: rows = x_train[y_train == label] ax.scatter(rows[:, 0], rows[:, 1], color=color, label=f"Train {label:+d}") # 空心圈标出间隔线上的训练点,黑叉用来检查留出点位于边界哪一侧。 ax.scatter(*x_train[support].T, facecolors="none", edgecolors="black", s=170, label="Margin points") ax.scatter(*x_test.T, color="black", marker="x", label="Held-out samples") ax.set(xlabel="Feature 1", ylabel="Feature 2", aspect="equal", title=f"Linear hard-margin SVM (width = {width:g})") ax.legend(fontsize=8) return { "weights": w.tolist(), "intercept": float(b), "margin_width": float(width), "min_training_margin": float(train_margin.min()), "support_rows": (np.flatnonzero(support) + 1).tolist(), "test_scores": score.tolist(), "test_predictions": predicted.tolist(), "test_accuracy": float(np.mean(predicted == y_test)), } if __name__ == "__main__": print("LAB_RESULT " + json.dumps(run(), allow_nan=False)) plt.show()