FastShap摘要图 - 错误:CAN CAN COMBINE< double> and< factor< 919a3>>

发布于 2025-01-18 15:43:37 字数 909 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2025-01-25 15:43:37

我发现您正在改编 Julia Silge 的预测棋盘游戏评级教程中的代码。原始代码使用 SHAPforxgboost 生成 SHAP 值,但您使用的是 fastshap 包。

由于 Shapley 解释最近才开始受到关注,因此标准数据格式并不多。 fastshap 不喜欢 tidyverse tibble,它只需要矩阵或类矩阵。

发生错误的原因是,默认情况下,fastshap 尝试将 tibble 转换为矩阵。但这失败了,因为矩阵只能有一种类型(fx 要么是 double 要么是 factor,不能两者兼而有之)。

我也遇到了类似的问题,发现您可以通过将 X 参数作为 data.frame。我无权访问您的完整代码,但您可以尝试将 shap_values_G 代码块替换为:

shap_values_G <- fastshap::explain(xgb_fit,
                  X = game_train,                   
                  pred_wrapper = 
                  p_function_G,
                  nsim = 50,
                  newdata= as.data.frame(game_train[which(game_test=="AntiSocial"),]))
) 

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 the shap_values_G code-block as so:

shap_values_G <- fastshap::explain(xgb_fit,
                  X = game_train,                   
                  pred_wrapper = 
                  p_function_G,
                  nsim = 50,
                  newdata= as.data.frame(game_train[which(game_test=="AntiSocial"),]))
) 

Wrap newdata with as.data.frame. This converts the tibble to a dataframe and so shouldn't upset fastshap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文