这是一段我遇到麻烦的代码:
pump_recipe <- recipe(status_group ~ ., data = data) %>%
step_impute_median(all_numeric_predictors()) %>%
step_impute_knn(all_nominal_predictors()) %>%
step_dummy(all_nominal_predictors()) %>%
step_normalize(all_numeric_predictors())
prepared_rec <- prep(pump_recipe)
错误:
Error:
! Column name `funder_W.D...I.` must not be duplicated.
Use .name_repair to specify repair.
Caused by error in `stop_vctrs()`:
! Names must be unique.
x These names are duplicated:
* "funder_W.D...I." at locations 1807 and 1808.
Backtrace:
1. recipes::prep(pump_recipe)
2. recipes:::prep.recipe(pump_recipe)
4. recipes:::bake.step_dummy(x$steps[[i]], new_data = training)
8. tibble:::as_tibble.data.frame(indicators)
9. tibble:::lst_to_tibble(unclass(x), .rows, .name_repair)
...
16. vctrs `<fn>`()
17. vctrs:::validate_unique(names = names, arg = arg)
18. vctrs:::stop_names_must_be_unique(names, arg)
19. vctrs:::stop_names(...)
20. vctrs:::stop_vctrs(class = c(class, "vctrs_error_names"), ...)
Error:
Caused by error in `stop_vctrs()`:
! Names must be unique.
x These names are duplicated:
* "funder_W.D...I." at locations 1807 and 1808.
所以基本上看起来step_dummy
步骤做了一些奇怪的事情,在这里创建了一个重复的列。我不知道为什么会这样。这是我正在处理的数据:
https://github.com/norhther/datasets/blob/main/data.csv
您的funder
和installer
中的水平非常相似,以至于step_dummy()
创建了相同名称的标签。错误提示funder_W.D...I.
出现两次
如果我们对funder
列进行一些过滤,我们会看到有3个不同的名称匹配。
str_subset(data$funder, "W.D") |> unique()
[1] "W.D.&.I." "W.D & I." "W.D &"
"W.D.&.I."
或"W.D & I."
都不是有效的名称,所以step_dummy()
尝试修复它们。这将为两者生成"funder_W.D...I."
。
您可以通过使用textrecipes::step_clean_levels()
来解决这个问题,这确保这些变量的级别保持有效且不重叠。
library(recipes)
pump_recipe <- recipe(status_group ~ ., data = data) %>%
step_impute_median(all_numeric_predictors()) %>%
step_impute_knn(all_nominal_predictors()) %>%
textrecipes::step_clean_levels(funder, installer) %>%
step_dummy(all_nominal_predictors()) %>%
step_normalize(all_numeric_predictors())
prepared_rec <- prep(pump_recipe)
注意:正如你所说,我会想象"W.D.&.I."
,"W.D & I."
和"W.D &"
都指的是同一个实体。你应该看看是否可以手动折叠这些关卡。