尝试使用tidymodels为分类问题生成形状值时出现问题。
当我试着在tidymodels中训练我的模型后计算形状值时,按照这个网站上的步骤https://github.com/ModelOriented/kernelshap,我不能报告它的分类问题。我的目标变量必须是因子。它总是返回:
check_pred(pred_fun(object,X,...),n = n)中的错误:运行函数kernelshap后预测必须为数值
我找到了用矩阵代替数据框架和用extract fit parsnip提取模型的方法。但问题仍然存在,有没有一种方法可以复制的例子,但分类。示例代码如下
library(tidyverse)
library(tidymodels)
Default <- ISLR::Default
Default = Default %>%
mutate(
default = factor(case_when(
default == "Yes" ~ 1,
default == "No" ~ 0
), levels = c(1,0)
)
)# changing to factor otherwise model will not work
# model fitting
split <- initial_split(Default)
tr <- training(split)
te <- testing(split)
rec <- recipe(default~., data = Default) %>%
step_dummy(all_nominal_predictors())
spec <- boost_tree() %>%
set_mode("classification") %>%
set_engine("xgboost")
wf <- workflow() %>%
add_model(spec) %>%
add_recipe(rec)
# model fit
mod <- fit(wf,tr)
library(kernelshap)
x <- rec %>%
prep %>%
bake(te %>%
slice_sample(n = 50)) %>%
select(-default) %>%
as.data.frame()
bg <- rec %>% prep %>%
bake(te %>% slice_sample(n = 10)) %>%
mutate(default = as.numeric(as.character(default))) %>%
as.data.frame()
# test for prediction
predict(mod, te)
# extract model form tidymodels
md <- extract_fit_parsnip(mod)
# this version works
kernelshap(md$fit,
X = x %>% as.matrix(), # if i do it with matrix structure then it works
bg_X =bg %>% as.matrix()
)
# this version does not work
kernelshap(mod,
X = te %>%
select(-default) %>% # remove target var
slice_sample(n = 50) %>%
as.data.frame(),
bg_X = te %>%
slice_sample(n = 50) %>%
as.data.frame()
)
################################### error messgae:
#Error in check_pred(pred_fun(object, X, ...), n = n) :
# Predictions must be numeric
######################################
kernelshap(mod,
X = te %>%
select(-default) %>% # remove target var
slice_sample(n = 50) %>%
as.data.frame(),
bg_X = te %>%
slice_sample(n = 50) %>%
as.data.frame()
)
# toy example from github page using tidymodels
library(tidymodels)
library(kernelshap)
iris_recipe <- iris %>%
recipe(Sepal.Length ~ .)
reg <- linear_reg() %>%
set_engine("lm")
iris_wf <- workflow() %>%
add_recipe(iris_recipe) %>%
add_model(reg)
fit <- iris_wf %>%
fit(iris)
ks <- kernelshap(fit, iris[, -1], bg_X = iris)
ks
字符串
1条答案
按热度按时间iyfamqjs1#
{kernelshap}旨在与Tidymodels配合使用。在这种情况下,你可以简单地写:
字符串
评论