DAY 20
浙大疏锦行“”完整的 SHAP 回归分析示例加州房价数据集使用 XGBoost 回归模型涵盖全部主要可视化方法。“”import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport shapimport xgboost as xgbfrom sklearn.datasets import fetch_california_housingfrom sklearn.model_selection import train_test_split1. 加载数据data fetch_california_housing()X pd.DataFrame(data.data, columnsdata.feature_names)y data.targetprint(“特征矩阵形状:”, X.shape)print(“特征名称:”, list(X.columns))2. 划分训练集和测试集X_train, X_test, y_train, y_test train_test_split(X, y, test_size0.2, random_state42)3. 训练 XGBoost 回归模型model xgb.XGBRegressor(n_estimators300,learning_rate0.05,max_depth6,n_jobs-1,random_state42)model.fit(X_train, y_train)print(f测试集 R² 得分: {model.score(X_test, y_test):.4f})4. 创建 TreeExplainer 并计算 SHAP 值注意为了演示速度我们只取测试集前 500 个样本X_sample X_test.iloc[:500]explainer shap.TreeExplainer(model)shap_values explainer.shap_values(X_sample) # numpy 数组, shape (500, 8)shap_values_obj explainer(X_sample) # shap.Explanation 对象, 用于新版APIprint(fSHAP 值矩阵形状: {shap_values.shape}“) # (500, 8)print(f基准值 (expected_value): {explainer.expected_value:.4f}”)5. 全局解释5.1 特征重要性条形图使用旧版 summary_plotplt.figure()shap.summary_plot(shap_values, X_sample, plot_type“bar”, showFalse)plt.title(“全局特征重要性 (条形图)”)plt.tight_layout()plt.show()5.2 使用新版 api 的条形图plt.figure()shap.plots.bar(shap_values_obj, max_display8)plt.title(“全局特征重要性 (bar plot 新版)”)plt.show()5.3 蜂群图 (summary plot)plt.figure()shap.summary_plot(shap_values, X_sample, showFalse)plt.title(“全局特征影响力 (蜂群图)”)plt.tight_layout()plt.show()5.4 蜂群图新版 beeswarmplt.figure()shap.plots.beeswarm(shap_values_obj, max_display8)plt.title(“蜂群图 (beeswarm)”)plt.show()6. 特征依赖图 (Dependence Plot)6.1 查看 MedInc 对房价的影响自动选择交互特征着色plt.figure()shap.dependence_plot(‘MedInc’, shap_values, X_sample, showFalse)plt.title(“MedInc 依赖图 (自动着色)”)plt.tight_layout()plt.show()6.2 指定交互特征 AveOccupplt.figure()shap.dependence_plot(‘MedInc’, shap_values, X_sample,interaction_index‘AveOccup’, showFalse)plt.title(“MedInc 依赖图 (用 AveOccup 着色)”)plt.tight_layout()plt.show()6.3 使用新版散点图 (scatter) 替代plt.figure()shap.plots.scatter(shap_values_obj[:, “MedInc”], colorshap_values_obj[:, “AveOccup”])plt.title(“散点图 (scatter) 显示交互”)plt.show()7. 局部解释单个样本的力图 (Force Plot)单个样本的力图显示在 Jupyter 中为交互式HTML这里用 matplotlib 模式但 matplotlib 模式效果欠佳推荐在 Jupyter 中执行此处保存为 HTML 并显示静态图try:# 尝试使用 matplotlib 模式保存为图片shap.force_plot(explainer.expected_value,shap_values[0, :],X_sample.iloc[0, :],matplotlibTrue,showFalse)plt.title(“单个样本的力图”)plt.tight_layout()plt.show()except:print(“force_plot matplotlib 模式可能不稳定建议在 Jupyter 中交互式查看”)也可以生成 HTML 并保存不在 Jupyter 中直接显示force_html shap.force_plot(explainer.expected_value,shap_values[0, :],X_sample.iloc[0, :],showFalse)保存为 HTML 文件shap.save_html(‘force_plot_sample.html’, force_html)print(“单个样本力图已保存为 force_plot_sample.html”)8. 瀑布图 (Waterfall Plot)瀑布图需要 shap.Explanation 对象的单行plt.figure()shap.plots.waterfall(shap_values_obj[0], max_display8)plt.title(“单个样本瀑布图”)plt.show()9. 决策图 (Decision Plot)绘制前20个样本的决策路径plt.figure()shap.decision_plot(explainer.expected_value,shap_values[:20, :],X_sample.iloc[:20, :],showFalse)plt.title(“前20个样本的决策图”)plt.tight_layout()plt.show()10. 热图 (Heatmap)plt.figure()shap.plots.heatmap(shap_values_obj, max_display8)plt.title(“SHAP 值热图”)plt.show()11. 额外查看单个特征的散点分布scatter plot用于显示特征值与 SHAP 值关系plt.figure()shap.plots.scatter(shap_values_obj[:, “MedInc”])plt.title(“MedInc 的 SHAP 值散点图”)plt.show()打印各图的形状要求说明仅供查阅print(“\n SHAP 绘图函数形状要求速查 ”)print(“force_plot单样本: shap_values (M_features,), features (M_features,)”)print(“force_plot多样本: shap_values (N_samples, M_features), features (N_samples, M_features)”)print(“summary_plot: shap_values (N_samples, M_features), features (N_samples, M_features)”)print(“dependence_plot: shap_values (N_samples, M_features), features (N_samples, M_features)”)print(“waterfall: shap.Explanation 单行即 shap_values_obj[0]”)print(“decision_plot: shap_values (N_samples, M_features), features (N_samples, M_features)”)print(“heatmap: shap.Explanation 多行即 shap_values_obj”)print(“scatter: shap.Explanation需指定特征列”)