如何移除shapviz生成的SHAP瀑布图Other Features并调整E[f(x)]位置
解决方案:shapviz瀑布图定制(移除Other特征+调整E[f(x)]位置)
1. 移除“other features”并保留5个特征的瀑布图
默认sv_waterfall在设置max_display=5时会自动添加“other features”行汇总剩余特征。要移除它,需要过滤绘图数据并更新y轴刻度:
library(shapviz) library(ggplot2) # 生成原始瀑布图 g <- sv_waterfall( shap, row_id = 25, max_display = 5, fill_colors = c("#e0115f", "#0096ff"), annotation_size = 5 ) # 过滤掉other features行 g$data <- g$data[!g$data$feature %in% "other features", ] # 更新y轴的因子范围和刻度,确保只显示5个特征 y_scale <- g$scales$scales[[2]] # 定位y轴刻度对象 y_scale$breaks <- y_scale$breaks[!y_scale$breaks %in% "other features"] y_scale$limits <- y_scale$limits[!y_scale$limits %in% "other features"] # 输出定制后的图 print(g)
2. 调整max_display=10时E[f(x)]的位置
当显示10个特征时,E[f(x)]的注释位置可通过修改对应图层的x坐标调整:
# 生成10个特征的瀑布图 g_10 <- sv_waterfall( shap, row_id = 25, max_display = 10, fill_colors = c("#e0115f", "#0096ff"), annotation_size = 5 ) # 定位E[f(x)]注释图层(通常为layers[[4]],可通过str(g_10$layers)确认) # 调整x坐标为基础值位置,或自定义数值(比如shap$baseline - 0.5) g_10$layers[[4]]$data$x <- shap$baseline # 输出调整后的图 print(g_10)
进阶:完全手动构造瀑布图(更灵活的控制)
如果需要更精细的定制,可手动计算SHAP数据并使用ggplot直接绘制,彻底避免默认逻辑的限制:
# 提取目标行的SHAP值与特征名 shap_row <- shap$S[25, ] feature_names <- colnames(shap$S) # 按SHAP绝对值排序,取前5个特征 top5_idx <- order(-abs(shap_row))[1:5] top5_shap <- shap_row[top5_idx] top5_features <- feature_names[top5_idx] # 构造瀑布图数据:基础值+特征SHAP+最终预测值 baseline <- shap$baseline final_pred <- baseline + sum(top5_shap) waterfall_data <- data.frame( feature = c("E[f(x)]", rev(top5_features), "f(x)"), value = c(baseline, rev(top5_shap), final_pred), cumulative = c(baseline, baseline + cumsum(rev(top5_shap)), final_pred), type = c("baseline", rep("feature", 5), "final") ) # 绘制自定义瀑布图 ggplot(waterfall_data, aes(x = cumulative, y = reorder(feature, -row_number()), fill = type)) + geom_bar(stat = "identity") + geom_text( aes(label = round(value, 2)), hjust = ifelse(waterfall_data$value >= 0, 1.1, -0.1), size = 5 ) + scale_fill_manual( values = c("baseline" = "gray", "feature" = c("#e0115f", "#0096ff"), "final" = "black") ) + labs(x = "SHAP Value", y = "Feature") + theme_minimal()
内容的提问来源于stack exchange,提问作者Ornella Scardua
相关产品推荐
相关产品推荐

