食谱::step_dummy + caret::train ->错误:并非配方中的所有变量都存在 [英] recipes::step_dummy + caret::train -> Error:Not all variables in the recipe are present

查看:54
本文介绍了食谱::step_dummy + caret::train ->错误:并非配方中的所有变量都存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用带有 caret::train 的 recipes::step_dummy 时出现以下错误(第一次尝试组合两个包):

I am getting the following error when using recipes::step_dummy with caret::train (first attempt at combining the two packages):

错误:并非配方中的所有变量都存在于提供的训练集

Error: Not all variables in the recipe are present in the supplied training set

不确定是什么导致了错误,也不确定调试的最佳方法.帮助训练模型将不胜感激.

Not sure what is causing the error nor the best way to debug. Help to train model would be much appreciated.

library(caret)
library(tidyverse)
library(recipes)
library(rsample)

data("credit_data")

## Split the data into training (75%) and test sets (25%)
set.seed(100)
train_test_split <- initial_split(credit_data)
credit_train <- training(train_test_split)
credit_test <- testing(train_test_split)

# Create recipe for data pre-processing
rec_obj <- recipe(Status ~ ., data = credit_train) %>%
  step_knnimpute(all_predictors()) %>%
  #step_other(Home, Marital, threshold = .2, other = "other") %>%
  #step_other(Job, threshold = .2, other = "others") %>%
  step_dummy(Records)  %>% 
  step_center(all_numeric())  %>%
  step_scale(all_numeric()) %>%
  prep(training = credit_train, retain = TRUE) 

train_data <- juice(rec_obj)
test_data  <- bake(rec_obj, credit_test)

set.seed(1055)
# the glm function models the second factor level.
lrfit <- train(rec_obj, data = train_data,
                     method = "glm",
                     trControl = trainControl(method = "repeatedcv", 
                                              repeats = 5))

推荐答案

在将配方提供给 train 之前不要准备配方并使用原始训练集:

Don't prep the recipe before giving it to train and use the original training set:

library(caret)
#> Loading required package: lattice
#> Loading required package: ggplot2
library(tidyverse)
library(recipes)
#> 
#> Attaching package: 'recipes'
#> The following object is masked from 'package:stringr':
#> 
#>     fixed
#> The following object is masked from 'package:stats':
#> 
#>     step
library(rsample)

data("credit_data")

## Split the data into training (75%) and test sets (25%)
set.seed(100)
train_test_split <- initial_split(credit_data)
credit_train <- training(train_test_split)
credit_test <- testing(train_test_split)

# Create recipe for data pre-processing
rec_obj <- 
  recipe(Status ~ ., data = credit_train) %>%
  step_knnimpute(all_predictors()) %>%
  #step_other(Home, Marital, threshold = .2, other = "other") %>%
  #step_other(Job, threshold = .2, other = "others") %>%
  step_dummy(Records)  %>% 
  step_center(all_numeric())  %>%
  step_scale(all_numeric()) 

set.seed(1055)
# the glm function models the second factor level.
lrfit <- train(rec_obj, data = credit_train,
               method = "glm",
               trControl = trainControl(method = "repeatedcv", 
                                        repeats = 5))
lrfit
#> Generalized Linear Model 
#> 
#> 3341 samples
#>   13 predictor
#>    2 classes: 'bad', 'good' 
#> 
#> Recipe steps: knnimpute, dummy, center, scale 
#> Resampling: Cross-Validated (10 fold, repeated 5 times) 
#> Summary of sample sizes: 3006, 3008, 3007, 3007, 3007, 3007, ... 
#> Resampling results:
#> 
#>   Accuracy   Kappa    
#>   0.7965349  0.4546223

reprex 包 (v0.2.1) 于 2019 年 3 月 20 日创建

Created on 2019-03-20 by the reprex package (v0.2.1)

这篇关于食谱::step_dummy + caret::train ->错误:并非配方中的所有变量都存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆