"""高斯朴素贝叶斯用类别先验和逐列似然计算后验概率。""" import json import matplotlib.pyplot as plt import numpy as np def posterior(query, means, variances, prior): # 每条查询分别减两类的中心,对特征轴求和得到每类的对数似然。 # 扩展后的差值数组形状为“查询数 × 类别数 × 特征数”。 deviation = query[:, None, :] - means[None, :, :] # 条件独立让各特征的类条件密度相乘,取对数后沿特征轴相加。 log_likelihood = -0.5 * np.sum( np.log(2 * np.pi * variances) + deviation**2 / variances, axis=2 ) # 对数先验与对数似然相加得到联合分数。先验数组按类 0、类 1 排列。 log_joint = np.log(prior) + log_likelihood # 每行先减最大值,指数相对大小不变,避免小概率连乘下溢。 scaled = np.exp(log_joint - log_joint.max(axis=1, keepdims=True)) return scaled / scaled.sum(axis=1, keepdims=True) def run(): # 人工布置有重叠的两类传感器读数,各类每列均有变化。 train = np.array([[1, 2], [1.5, 2.5], [2, 1.5], [2.5, 2.2], [3, 3.5], [3.3, 2.8], [3, 3], [3.5, 4], [4.5, 3.5], [5, 5], [4.2, 4.8], [5.4, 4.4]]) y_train = np.repeat([0, 1], 6) test = np.array([[1.5, 2.0], [3.1, 3.25], [3.4, 3.4], [4.8, 4.4]]) y_test = np.array([0, 0, 1, 1]) # 各类独立估计参数:极大似然方差除以本类样本数,而不是 n_c - 1。 class_rows = [train[y_train == label] for label in [0, 1]] means = np.array([rows.mean(axis=0) for rows in class_rows]) variances = np.maximum([rows.var(axis=0, ddof=0) for rows in class_rows], 1e-9) # 先用训练样本比例,再模拟正类更少的场景;两组都复用同一组似然参数。 priors = np.array([np.bincount(y_train) / len(y_train), [0.8, 0.2]]) # 后验每列对应一个类,取下标 1 得到正类;结果每行对应一种先验。 probability = np.array([posterior(test, means, variances, p)[:, 1] for p in priors]) predicted = probability > 0.5 # 恰好并列时返回类 0 # 用网格绘制概率场和 0.5 的决策边界。只变化类别先验。 a, b = np.meshgrid(np.linspace(0.5, 5.8, 130), np.linspace(1, 5.5, 125)) grid = np.column_stack((a.ravel(), b.ravel())) fig, axes = plt.subplots(1, 2, figsize=(8.6, 3.8), layout="constrained") for ax, prior in zip(axes, priors): surface = posterior(grid, means, variances, prior)[:, 1].reshape(a.shape) filled = ax.contourf(a, b, surface, levels=np.linspace(0, 1, 11), cmap="PuBu") ax.contour(a, b, surface, levels=[0.5], colors="black", linewidths=1.4) for label, color in [(0, "C1"), (1, "C0")]: ax.scatter(*train[y_train == label].T, color=color, edgecolors="white", label=f"Train class {label}") ax.scatter(*test.T, color="black", marker="x", label="Test queries") ax.set(xlabel="Sensor 1", ylabel="Sensor 2", title=f"Prior P(1) = {prior[1]:g}") axes[0].legend(fontsize=8) fig.colorbar(filled, ax=axes, label="Posterior P(1 | x)") # 比较留出记录上的类标及概率。后验的可靠性还依赖类内高斯和独立假设。 return { "class_means": means.tolist(), "class_variances": variances.tolist(), "priors": priors.tolist(), "test_positive_probability": probability.tolist(), "test_predictions": predicted.astype(int).tolist(), "test_accuracy": np.mean(predicted == y_test, axis=1).tolist(), } if __name__ == "__main__": print("LAB_RESULT " + json.dumps(run(), allow_nan=False)) plt.show()