尝试将训练数据中的 tidymodels 配方应用到 r 中的重采样时出错?

如何解决尝试将训练数据中的 tidymodels 配方应用到 r 中的重采样时出错?

我是 tidymodels 的新手,对 R 也有些陌生。我正在尝试从 Youtube tidytuesday/Sliced Customer churn data 中复制 David Robinson 的代码,但在对 交叉验证数据应用配方更改时面临问题/ 重新采样

问题:当我对训练数据执行step_mutate()时,它会起作用,但是当我应用相同的配方时strong> 对 交叉验证 train_5folds 数据然后给出错误Error: All of the models Failed. See the .notes column.

重现问题(使用以下代码下载数据):

train <- read.csv(url("https://raw.githubusercontent.com/johnsNow09/covid19-df_stack-code/main/train_object.csv"))

train_5fold 可以从以下位置下载交叉验证的重采样数据:https://github.com/johnsnow09/covid19-df_stack-code/blob/main/train_5fold.RDS

train_5fold <- readRDS("train_5fold.RDS")

代码

library(tidyverse)
library(tidymodels)
mset <- metric_set(mn_log_loss)

control <- control_grid(save_workflow = TRUE,save_pred = TRUE,extract = extract_model)
xg_spec <- parsnip::boost_tree(
    trees = tune(),mtry = tune(),learn_rate = tune()) %>% 
  set_engine("xgboost") %>%
  set_mode("classification")


factor_to_ordinal <- function(x){
  ifelse(x == "UnkNown",NA,as.integer(x))
}


xg_rec_4 <- recipe(churned ~  .,data = train) %>% 
  
  update_role(id,new_role = "ID") %>%
  step_mutate(income_category = factor_to_ordinal(income_category),education_level = factor_to_ordinal(education_level)) %>% 
  step_impute_mean(all_numeric_predictors()) %>% 
  step_dummy(all_nominal_predictors())


xg_wf_4 <- workflow() %>% 
  add_recipe(xg_rec_4) %>% 
  add_model(xg_spec)


xg_res_4 <- xg_wf_4 %>%  
  tune_grid(
    resamples = train_5fold,metrics = mset,control = control,grid = crossing(trees = seq(200,800,20),mtry = c(2,4,6,8,10),learn_rate = c(0.02))
    )
  )

autoplot(xg_res_4)

错误:所有模型都失败了。请参阅 .notes 列。

.notes我得到

.notes
<chr>
preprocessor 1/1: Error: Problem with `mutate()` column `income_category`.\ni `income_category = factor_to_ordinal(income_category)`.\nx Could not find function "factor_to_ordinal"

交叉检查:

xg_rec_4 %>% prep() %>% juice()

# A tibble: 5,316 x 15
      id customer_age education_level income_category total_relationship~ months_inactive_1~ credit_limit
   <dbl>        <dbl>           <int>           <int>               <dbl>              <dbl>        <dbl>
 1  9168           46               3               5                   3                  3         2171
 2  2187           51               4               4                   3                  1        11373
 3  5659           48               3               4                   4                  2        14322
 4   447           57               6               2                   5                  3        12291
 5  6342           39               4               5                   5                  2         1862
 6   496           56               6               5                   4                  3         3219
 7  7064           33               4               1                   6                  3        27499
 8  3978           48               4               4                   1                  2        34516
 9    13           41               4               5                   4                  3         2372
10  8242           46               3               2                   4                  3         3115
# ... with 5,306 more rows,and 8 more variables: total_revolving_bal <dbl>,total_amt_chng_q4_q1 <dbl>,#   total_trans_amt <dbl>,total_trans_ct <dbl>,total_ct_chng_q4_q1 <dbl>,avg_utilization_ratio <dbl>,#   churned <fct>,gender_M <dbl>
colSums(xg_rec_4 %>% prep() %>% juice() %>% select_if(is.numeric) %>% is.na())

                      id             customer_age          education_level          income_category 
                       0                        0                        0                        0 
total_relationship_count   months_inactive_12_mon             credit_limit      total_revolving_bal 
                       0                        0                        0                        0 
    total_amt_chng_q4_q1          total_trans_amt           total_trans_ct      total_ct_chng_q4_q1 
                       0                        0                        0                        0 
   avg_utilization_ratio                 gender_M 
                       0                        0 

在视频中它适用于大卫罗宾逊的地方:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?