1 import numpy as np 2 import matplotlib.pyplot as plt 3 from matplotlib.gridspec import GridSpec 4 from _utils.plot_utils import setup_style, save_fig, PALETTE, COLORS, _lighten 5 6 setup_style() 7 np.random.seed(42) 8 9 # === 模拟 ICE 曲线(200 个样本,特征 x 从 0 到 10)=== 10 n_samples = 200 11 x_grid = np.linspace(0, 10, 80) 12 # 每个样本的随机基线 + 非线性形状 13 baselines = np.random.normal(0.5, 0.15, n_samples) 14 shape_strength = np.random.normal(1.0, 0.25, n_samples) 15 ice_curves = np.array([ 16 baselines[i] + shape_strength[i] * (0.45 / (1 + np.exp(-1.2*(x_grid - 4))) + 0.08 * np.sin(x_grid * 0.6)) 17 + np.random.normal(0, 0.02, len(x_grid)) 18 for i in range(n_samples) 19 ]) 20 pdp = ice_curves.mean(axis=0) 21 pdp_ci_lo = np.percentile(ice_curves, 5, axis=0) 22 pdp_ci_hi = np.percentile(ice_curves, 95, axis=0) 23 24 # 样本特征分布(边际) 25 x_samples = np.random.beta(2.5, 2.0, n_samples * 3) * 10 # 中心偏左的分布 26 27 # === 双面板:上 ICE+PDP,下 边际分布 === 28 fig = plt.figure(figsize=(7, 5.2)) 29 gs = GridSpec(2, 1, height_ratios=[4, 1], hspace=0.04) 30 ax = fig.add_subplot(gs[0]) 31 ax_marg = fig.add_subplot(gs[1], sharex=ax) 32 33 # ★ ICE 细线(每条 alpha 极低,重叠出"密度感") 34 for i in range(n_samples): 35 ax.plot(x_grid, ice_curves[i], color=PALETTE[0], linewidth=0.5, alpha=0.06, zorder=1) 36 37 # ★ PDP 95% CI 带(外层浅、内层稍深) 38 ax.fill_between(x_grid, pdp_ci_lo, pdp_ci_hi, color=_lighten(PALETTE[0], 0.55), 39 alpha=0.45, linewidth=0, zorder=2, label='90% 区间') 40 41 # ★ PDP 中位数线 — 主信号 42 ax.plot(x_grid, pdp, color=PALETTE[0], linewidth=2.6, zorder=3, label='PDP (均值)') 43 # 加白色描边让曲线在 ICE 海里更突出 44 ax.plot(x_grid, pdp, color='white', linewidth=4.5, zorder=2.5, alpha=0.6) 45 ax.plot(x_grid, pdp, color=PALETTE[0], linewidth=2.6, zorder=3) 46 47 # ★ 拐点标注 48 turn_idx = np.argmax(np.abs(np.gradient(pdp))) 49 ax.scatter([x_grid[turn_idx]], [pdp[turn_idx]], s=80, color=COLORS['highlight'], 50 edgecolor='white', linewidth=1.5, zorder=5, marker='o') 51 ax.annotate(f'拐点\n(x={x_grid[turn_idx]:.1f})', 52 xy=(x_grid[turn_idx], pdp[turn_idx]), 53 xytext=(15, 18), textcoords='offset points', 54 fontsize=8, fontweight='bold', color=COLORS['highlight'], 55 arrowprops=dict(arrowstyle='->', color=COLORS['highlight'], lw=1.1, alpha=0.7), 56 bbox=dict(boxstyle='round,pad=0.3', facecolor='white', 57 edgecolor=COLORS['highlight'], alpha=0.9, linewidth=0.6)) 58 59 ax.set_ylabel('预测值 (P(y=1))', fontsize=10) 60 ax.legend(loc='lower right', frameon=False, labelspacing=0.35, handlelength=1.6, fontsize=8) 61 ax.spines['top'].set_visible(False) 62 ax.spines['right'].set_visible(False) 63 ax.tick_params(axis='x', labelbottom=False) 64 ax.grid(axis='y', alpha=0.1, linestyle='--', color=COLORS['grid']) 65 66 # ★ 底部 rug + 直方图:告诉读者 PDP 在哪些 x 上有数据 67 ax_marg.hist(x_samples, bins=40, color=_lighten(PALETTE[0], 0.4), alpha=0.85, 68 edgecolor=PALETTE[0], linewidth=0.4) 69 ax_marg.set_xlabel('特征 x', fontsize=10) 70 ax_marg.set_ylabel('样本密度', fontsize=8) 71 ax_marg.spines['top'].set_visible(False) 72 ax_marg.spines['right'].set_visible(False) 73 ax_marg.tick_params(axis='y', labelsize=7) 74 75 save_fig(fig, 'figures/fig_ice_pdp.pdf')