"""训练一元逻辑回归,再在固定留出样本上比较两个阈值。""" import json # NumPy 做向量化的概率和梯度计算;Matplotlib 绘制决策曲线。 import matplotlib.pyplot as plt import numpy as np def sigmoid(score): # exp(-logaddexp(0, -score)) 等价于 1/(1+exp(-score)), # 当 score 很负时避免 exp(-score) 溢出。 return np.exp(-np.logaddexp(0, -score)) def run(): # 合成的已居中特征。相同分数在训练集中可以有不同标签。 x_train = np.array([-3, -2, -1, -1, 0, 0, 1, 1, 2, 3], dtype=float) y_train = np.array([0, 0, 0, 1, 0, 1, 0, 1, 1, 1], dtype=float) # 四条留出记录只参与评估,不进入下面的梯度迭代。 x_test = np.array([-2.5, -0.5, 0.5, 2.5]) y_test = np.array([0, 0, 1, 1]) design = np.column_stack((np.ones_like(x_train), x_train)) # 每行的第一列固定为 1,乘上截距后和斜率项相加。 # 正则化系数只作用于斜率。每轮记录更新前的目标值。 penalty, step = 0.1, 0.25 beta = np.zeros(2) losses = [] # beta 的两个分量依次为截距、斜率;到达梯度容差或预算用完时结束。 for _ in range(2000): score = design @ beta # 二元负对数似然的稳定形式是 softplus(score) - label*score。 losses.append( np.mean(np.logaddexp(0, score) - y_train * score) + penalty / 2 * beta[1] ** 2 ) # p-y 是逐样本导数;乘设计矩阵的转置,将训练行汇总为两个参数的导数。 gradient = design.T @ (sigmoid(score) - y_train) / len(y_train) gradient[1] += penalty * beta[1] if np.max(np.abs(gradient)) < 1e-10: break beta -= step * gradient thresholds = np.array([0.5, 0.7]) probability = sigmoid(beta[0] + beta[1] * x_test) # (1, 4) 与 (2, 1) 广播比较,同一组概率得到两种阈值预测。 predicted = probability[None, :] >= thresholds[:, None] # 行对应阈值。漏报和误报分开统计,避免准确率遮盖两类错误。 false_negatives = np.sum(~predicted & (y_test == 1), axis=1) false_positives = np.sum(predicted & (y_test == 0), axis=1) accuracy = np.mean(predicted == y_test, axis=1) result = { "coefficients": beta.tolist(), "test_probability": probability.tolist(), "thresholds": thresholds.tolist(), "test_accuracy": accuracy.tolist(), "false_negatives": false_negatives.tolist(), "false_positives": false_positives.tolist(), "training_objective": losses[-1], } # 左轴看概率曲线与阈值交点;右轴查正则化目标是否下降并趋平。 grid = np.linspace(-3.2, 3.2, 200) fig, (left, right) = plt.subplots(1, 2, figsize=(9, 3.8), layout="constrained") left.plot(grid, sigmoid(beta[0] + beta[1] * grid), label="Fitted probability") left.scatter(x_train, y_train, label="Training label", marker="x") left.scatter(x_test, probability, label="Test probability", facecolors="none", edgecolors="C2") for threshold in thresholds: left.axhline(threshold, linewidth=1, linestyle="--", label=f"Threshold {threshold:g}") left.set(xlabel="Feature score", ylabel="Probability or label", ylim=(-0.05, 1.05)) left.legend(fontsize=8) right.plot(np.arange(1, len(losses) + 1), losses) right.set(xlabel="Iteration", ylabel="Penalized mean log loss", xscale="log") fig.suptitle("Logistic regression") return result if __name__ == "__main__": print("LAB_RESULT " + json.dumps(run(), allow_nan=False)) plt.show()