Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中使用Python包(带有“网状结构”)_Python_R_Machine Learning_Reticulate - Fatal编程技术网

在R中使用Python包(带有“网状结构”)

在R中使用Python包(带有“网状结构”),python,r,machine-learning,reticulate,Python,R,Machine Learning,Reticulate,我试着在这里学习这个教程: 本教程介绍如何使用一种称为“SHAP”的机器学习算法,该算法试图为用户提供一种解释复杂的“黑盒”式算法结果的方法 在本教程之后,我能够完成所有工作——除了最后的“力图”。我已经在底部提供了我正在使用的代码。有人能帮我弄清楚为什么这些力图不起作用吗 library(modeldata) library(tidymodels) library(tidyverse) library(doParallel) library(probably) library(gt) dat

我试着在这里学习这个教程:

本教程介绍如何使用一种称为“SHAP”的机器学习算法,该算法试图为用户提供一种解释复杂的“黑盒”式算法结果的方法

在本教程之后,我能够完成所有工作——除了最后的“力图”。我已经在底部提供了我正在使用的代码。有人能帮我弄清楚为什么这些力图不起作用吗

library(modeldata)
library(tidymodels)
library(tidyverse)
library(doParallel)
library(probably)
library(gt)

data("credit_data")

data("credit_data")

credit_data <- credit_data %>%
  drop_na()

set.seed(12)

# initial split
split <- initial_split(credit_data, prop = 0.75, strata = "Status")

# train/test sets
train <- training(split)
test <- testing(split)

rec <- recipe(Status ~ ., data = train) %>%
  step_bagimpute(Home, Marital, Job, Income, Assets, Debt) %>%
  step_dummy(Home, Marital, Records, Job, one_hot = T)

# Just some sensible values, not optimised by any means!
mod <- boost_tree(trees = 500,
                  mtry = 6,
                  min_n = 10,
                  tree_depth = 5) %>%
  set_engine("xgboost") %>%
  set_mode("classification")

xgboost_wflow <- workflow() %>%
  add_recipe(rec) %>%
  add_model(mod) %>%
  fit(train)

xg_res <- last_fit(xgboost_wflow,
                   split,
                   metrics = metric_set(roc_auc, pr_auc, accuracy))

preds <- xg_res %>%
  collect_predictions()

xg_res %>%
  collect_metrics()

library(vip)

# Get our model object
xg_mod <- pull_workflow_fit(xgboost_wflow)

vip(xg_mod$fit)

library(fastshap)

# Apply the preprocessing steps with prep and juice to the training data
X <- prep(rec, train) %>%
  juice() %>%
  select(-Status) %>%
  as.matrix()

# Compute shapley values
shap <- explain(xg_mod$fit, X = X, exact = TRUE)

# Create a dataframe of our training data
feat <- prep(rec, train) %>%
  juice()

autoplot(shap,
         type = "dependence",
         feature = "Amount",
         X = feat,
         smooth = TRUE,
         color_by = "Status")

predict(xgboost_wflow, train, type = "prob") %>%
  rownames_to_column("rowid") %>%
  filter(.pred_bad == min(.pred_bad) | .pred_bad == max(.pred_bad)) %>%
  gt()%>%
  fmt_number(columns = 2:3,
             decimals = 3)

library(patchwork)
p1 <- autoplot(shap, type = "contribution", row_num = 1541) +
  ggtitle("Likely bad")

p2 <- autoplot(shap, type = "contribution", row_num = 1806) +
  ggtitle("Likely good")

p1+p2

# here is the error (prior to running this code, I ran "pip install shap" in conda)

force_plot(object = shap[1541,],
           feature_values = X[1541,],
           display = "html",
           link = "logit")

Error in py_call_impl(callable, dots$args, dots$keywords) :
  TypeError: save_html() got an unexpected keyword argument 'plot_html'
库(modeldata)
图书馆(tidymodels)
图书馆(tidyverse)
图书馆(双平行)
图书馆(可能)
图书馆(gt)
数据(“信用数据”)
数据(“信用数据”)
信贷单位数据%
滴_na()
种子(12)
#初始分割
拆分%
添加_型号(mod)%%>%
装配(列车)
xg_res%
收集度量值()
图书馆(vip)
#获取我们的模型对象
xg_mod%
选择(-Status)%%>%
as.matrix()
#计算shapley值
形状%
行名称到列(“行ID”)%>%
过滤器(.pred_bad==min(.pred_bad)|。pred_bad==max(.pred_bad))%>%
gt()%>%
fmt_编号(列=2:3,
小数=3)
图书馆(拼凑)
p1
force_plot()
是一种实验性的,只是碰巧起了作用。如果收到错误,请确保已安装相应的
shap
包(及其依赖项)。在任何情况下,您都应该在fastshapGitHub repo:上报告此问题


--BG

@IRTFM:这有用吗?(在谷歌上搜索并点击第一个链接:“harry fisher shap machine learning”“)它确实“有效”,但随后似乎需要更多的“工作”。除了代码顶部的包之外,还有许多其他包丢失了,您没有提供任何代码来帮助这一过程。如果您包含了
的部分(!require(vip)){install.packages('vip');library(vip)}
和其他类似的“深奥”软件包,额外的“工作”可能会更少“为了达到一个共同的起点。我在犯了许多错误后筋疲力尽。你可能想返回并在之前问题的任何提交的答案上添加复选标记。仔细阅读你所有问题两侧的所有0,没有任何迹象表明你对任何人的答案投了赞成票,这给我的印象是,你并没有真正“玩游戏”这是一种认可。