随机森林模型的Shap图表

4

我想获得一个Ranger / Random Forest模型的Shap值,以及像这样的可视化图形:

beeswarm plots

我尝试使用如下库:DALEX,shapr,fastshap,shapper。但最后只能得到像这样的图像: fastshap plot

是否可能得到这样的图像呢?我已经尝试了reticulate包,但仍无法实现。


这篇文章看起来很有前途:https://www.r-statistics.com/2011/03/beeswarm-boxplot-and-plotting-it-with-r/ - Jonny Phelps
1个回答

3

随机森林需要生长许多深度树。虽然可能,但是对于深度树来说,计算TreeSHAP需要大量的内存和CPU功率。另一种选择是使用Kernel SHAP算法,它适用于所有类型的模型。

library(ranger)
library(kernelshap)
library(shapviz)

set.seed(1)
fit <- ranger(Sepal.Length ~ ., data = iris,)

# Step 1: Calculate Kernel SHAP values
# bg_X is usually a small (50-200 rows) subset of the data
s <- kernelshap(fit, iris[-1], bg_X = iris)

# Step 2: Turn them into a shapviz object
sv <- shapviz(s)

# Step 3: Gain insights...
sv_importance(sv, kind = "bee")
sv_dependence(sv, v = "Petal.Length", color_var = "auto")

SHAP summary plot SHAP dependence plot

免责声明: "kernelshap" 和 "shapviz" 是我编写的


1
从数据中排除响应变量("Sepal.Length")似乎是正确的。最近,{treeshap}已经在CRAN上发布。你可以使用它进行数据处理,然后使用{shapviz}绘制图表。 - undefined
1
你不能简单地将其转换为整数并设置probability = TRUE吗? - undefined
1
我已经尝试过了,但最好将classification = TRUE,否则在尝试使用treeshap包计算shap值时会出现一些错误("> rf= ranger.unify(fit, train_data) Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent")。 - undefined
好知道,谢谢! - undefined
1
我通常选择1000行来解释(X),并选择100-500行作为背景数据(bg_X)。随机森林的预测非常耗费资源,所以你甚至可以稍微减小数据集的大小。我认为这样做不会错过任何重要的内容。状态栏应该与X的大小完全线性相关,而背景数据的大小会导致内存占用和计算时间增加。 - undefined
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接