FastShap摘要图 - 错误:CAN CAN COMBINE< double> and< factor< 919a3>>
我正在尝试使用 fastshap 解释函数获取摘要图,如下面的代码所示。
p_function_G<- function(object, newdata)
caret::predict.train(object,
newdata =
newdata,
type = "prob")[,"AntiSocial"] # select G class
# Calculate the Shapley values
#
# boostFit: is a caret model using catboost algorithm
# trainset: is the dataset used for bulding the caret model.
# The dataset contains 4 categories W,G,R,GM
# corresponding to 4 diferent animal behaviors
library(caret)
shap_values_G <- fastshap::explain(xgb_fit,
X = game_train,
pred_wrapper =
p_function_G,
nsim = 50,
newdata= game_train[which(game_test=="AntiSocial"),])
)
但是我收到错误
Error in 'stop_vctrs()': 无法结合纬度和性别<因素<919a3>>
出路何在?
I'm trying to get a summary plot using fastshap explain function as in the code below.
p_function_G<- function(object, newdata)
caret::predict.train(object,
newdata =
newdata,
type = "prob")[,"AntiSocial"] # select G class
# Calculate the Shapley values
#
# boostFit: is a caret model using catboost algorithm
# trainset: is the dataset used for bulding the caret model.
# The dataset contains 4 categories W,G,R,GM
# corresponding to 4 diferent animal behaviors
library(caret)
shap_values_G <- fastshap::explain(xgb_fit,
X = game_train,
pred_wrapper =
p_function_G,
nsim = 50,
newdata= game_train[which(game_test=="AntiSocial"),])
)
However I'm getting error
Error in 'stop_vctrs()':
can't combine latitude and gender <factor<919a3>>
What's the way out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现您正在改编 Julia Silge 的预测棋盘游戏评级教程中的代码。原始代码使用 SHAPforxgboost 生成 SHAP 值,但您使用的是 fastshap 包。
由于 Shapley 解释最近才开始受到关注,因此标准数据格式并不多。
fastshap
不喜欢 tidyverse tibble,它只需要矩阵或类矩阵。发生错误的原因是,默认情况下,
fastshap
尝试将 tibble 转换为矩阵。但这失败了,因为矩阵只能有一种类型(fx 要么是 double 要么是 factor,不能两者兼而有之)。我也遇到了类似的问题,发现您可以通过将
X
参数作为 data.frame。我无权访问您的完整代码,但您可以尝试将shap_values_G
代码块替换为:将
newdata
替换为as.data.frame< /代码>。这会将 tibble 转换为数据帧,因此不应扰乱
fastshap
。I see that you are adapting code from Julia Silge's Predict ratings for board games Tutorial. The original code used SHAPforxgboost for generating SHAP values, but you're using the fastshap package.
Because Shapley explanations are only recently starting to gain traction, there aren't very many standard data formats.
fastshap
does not like tidyverse tibbles, it only takes matrices or matrix-likes.The error occurs because, by default,
fastshap
attempts to convert the tibble to a matrix. But this fails, because matrices can only have one type (f.x. either double or factor, not both).I also ran into a similar issue and found that you can solve this by passing the
X
parameter as a data.frame. I don't have access to your full code but you could you try replacing theshap_values_G
code-block as so:Wrap
newdata
withas.data.frame
. This converts the tibble to a dataframe and so shouldn't upsetfastshap
.