代码之家  ›  专栏  ›  技术社区  ›  user1420372

配方::step_dummy+caret::train->错误:配方中并非所有变量都存在

  •  0
  • user1420372  · 技术社区  · 7 年前

    我在使用recipes::step_dummy with caret::train(首次尝试组合这两个软件包)时遇到以下错误:

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

    不确定是什么导致了错误,也不确定调试的最佳方式。如能帮助培训模特,将不胜感激。

    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))
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   topepo    7 年前

    在把菜谱交给别人之前不要先准备好 train 并使用原始训练集:

    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
    

    于2019年3月20日由 reprex package (v0.2.1)

        2
  •  0
  •   user1420372    7 年前

    似乎你仍然需要在训练函数中使用这个公式(尽管在配方中列出)?。。。

    glmfit <- train(Status ~ ., data = juice(rec_obj),
                         method = "glm",
                         trControl = trainControl(method = "repeatedcv", repeats = 5))