1

我遇到了一些奇怪的行为,使用一个配方和一个工作流来区分垃圾邮件和使用 naiveBayes 分类器的有效文本。我试图使用 tidymodels 和工作流程来复制本书第 4 章机器学习与 R 的结果:https ://github.com/PacktPublishing/Machine-Learning-with-R-Second-Edition/blob/master/Chapter %2004/MLwR_v2_04.r

虽然我能够在有add_variables()add_formula()没有工作流的情况下重现分析,但使用该add_recipe()函数的工作流不起作用。

library(RCurl)
library(tidyverse)
library(tidymodels)
library(textrecipes)
library(tm)
library(SnowballC) 
library(discrim) 


sms_raw <- getURL("https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/sms_spam.csv")
sms_raw <- read_csv(sms_raw)
sms_raw$type <- factor(sms_raw$type)

set.seed(123)
split <- initial_split(sms_raw, prop = 0.8, strata = "type")
nb_train_sms <- training(split)
nb_test_sms <- testing(split)

# Text preprocessing
reci_sms <- 
  recipe(type ~.,
         data = nb_train_sms) %>% 
  step_mutate(text = str_to_lower(text)) %>% 
  step_mutate(text = removeNumbers(text)) %>% 
  step_mutate(text = removePunctuation(text)) %>% 
  step_tokenize(text) %>% 
  step_stopwords(text, custom_stopword_source = stopwords()) %>% 
  step_stem(text) %>% 
  step_tokenfilter(text, min_times = 6, max_tokens = 1500) %>% 
  step_tf(text, weight_scheme = "binary") %>% 
  step_mutate_at(contains("tf"), fn =function(x){ifelse(x == TRUE, "Yes", "No")}) %>% 
  prep()


df_training <- juice(reci_sms)
df_testing <- bake(reci_sms, new_data = nb_test_sms)

nb_model <- naive_Bayes() %>% 
  set_engine("klaR") 

以下是三个实际产生有效输出的代码示例

# --------- works but slow -----
nb_fit <- nb_fit <- workflow() %>%
  add_model(nb_model) %>%
  add_formula(type~.) %>%
  fit(df_training)
nb_tidy_pred <- nb_fit %>% predict(df_testing)


# --------- works  -----
nb_fit <- nb_model %>% fit(type ~., df_training)
nb_tidy_pred <- nb_fit %>% predict(df_testing)


# --------- works  -----

nb_fit <- workflow() %>%
  add_model(nb_model) %>%
  add_variables(outcomes = type, predictors = everything()) %>%
  fit(df_training)

nb_tidy_pred <- nb_fit %>% predict(df_testing)

虽然以下代码不起作用

nb_fit <- workflow() %>%
  add_model(nb_model) %>%
  add_recipe(reci_sms) %>%
  fit(data = df_training)

nb_tidy_pred <- nb_fit %>% predict(df_testing)

它还会引发以下错误,但我不太明白使用时发生了什么rlang::last_error()

Not all variables in the recipe are present in the supplied training set: 'text'.
Run `rlang::last_error()` to see where the error occurred.

有人能告诉我我错过了什么吗?

4

1 回答 1

2

当您在工作流程中使用配方时,您可以将预处理步骤与模型拟合结合起来。在拟合该工作流程时,您需要使用配方所期望nb_train_sms的数据 ( ) 而不是欧洲防风草模型所期望的数据。

此外,不建议将准备好的配方传递给工作流,因此prep()在使用add_recipe().

library(RCurl)
library(tidyverse)
library(tidymodels)
library(textrecipes)
library(tm) 
library(discrim)

sms_raw <- getURL("https://raw.githubusercontent.com/stedy/Machine-Learning-with-R-datasets/master/sms_spam.csv")
sms_raw <- read_csv(sms_raw)
sms_raw$type <- factor(sms_raw$type)

set.seed(123)
split <- initial_split(sms_raw, prop = 0.8, strata = "type")
nb_train_sms <- training(split)
nb_test_sms <- testing(split)

# Text preprocessing
reci_sms <- 
  recipe(type ~.,
         data = nb_train_sms) %>% 
  step_mutate(text = str_to_lower(text)) %>% 
  step_mutate(text = removeNumbers(text)) %>% 
  step_mutate(text = removePunctuation(text)) %>% 
  step_tokenize(text) %>% 
  step_stopwords(text, custom_stopword_source = stopwords()) %>% 
  step_stem(text) %>% 
  step_tokenfilter(text, min_times = 6, max_tokens = 1500) %>% 
  step_tf(text, weight_scheme = "binary")  %>% 
  step_mutate_at(contains("tf"), fn = function(x){ifelse(x == TRUE, "Yes", "No")})

nb_model <- naive_Bayes() %>% 
  set_engine("klaR") 

nb_fit <- workflow() %>%
  add_model(nb_model) %>%
  add_recipe(reci_sms) %>%
  fit(data = nb_train_sms)
#> Warning: max_features was set to '1500', but only 1141 was available and
#> selected.

nb_tidy_pred <- nb_fit %>% predict(nb_train_sms)

reprex 包创建于 2021-04-19 (v1.0.0)

于 2021-04-19T18:01:14.960 回答