當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R recipes check_new_values 檢查新值

check_new_values 創建配方操作的規範,該規範將檢查變量是否包含新值。

用法

check_new_values(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  columns = NULL,
  ignore_NA = TRUE,
  values = NULL,
  skip = FALSE,
  id = rand_id("new_values")
)

參數

recipe

一個菜譜對象。該檢查將添加到該配方的操作序列中。

...

一個或多個選擇器函數用於選擇用於此檢查的變量。有關更多詳細信息,請參閱selections()

role

由於沒有創建新變量,因此此檢查未使用。

trained

... 中的選擇器是否已由 prep() 解析的邏輯。

columns

所選變量名稱的字符串。該字段是一個占位符,一旦使用 prep() 就會被填充。

ignore_NA

指示我們是否應該將缺失值視為值的邏輯。默認為 TRUE

values

包含允許值的命名列表。在由 prep.recipe() 計算之前,這是NULL

skip

一個合乎邏輯的。當bake() 烘焙食譜時是否應該跳過檢查?雖然所有操作都是在 prep() 運行時烘焙的,但某些操作可能無法對新數據進行(例如處理結果變量)。使用skip = TRUE時應小心,因為它可能會影響後續操作的計算。

id

此檢查唯一的字符串,用於識別它。

recipe 的更新版本,將新檢查添加到任何現有操作的序列中。

細節

如果任何檢查列確實包含在配方上調用 prep 時不包含的值,則此檢查將破壞 bake 函數。如果檢查通過,則數據不會發生任何更改。

整理

當您 tidy() 進行此檢查時,將返回包含 terms 列(選擇的選擇器或變量)的 tibble。

箱重

底層操作不允許使用案例權重。

也可以看看

其他檢查:check_class()check_cols()check_missing()check_range()

例子

data(credit_data, package = "modeldata")

# If the test passes, `new_data` is returned unaltered
recipe(credit_data) %>%
  check_new_values(Home) %>%
  prep() %>%
  bake(new_data = credit_data)
#> # A tibble: 4,454 × 14
#>    Status Seniority Home     Time   Age Marital Records Job       Expenses
#>    <fct>      <int> <fct>   <int> <int> <fct>   <fct>   <fct>        <int>
#>  1 good           9 rent       60    30 married no      freelance       73
#>  2 good          17 rent       60    58 widow   no      fixed           48
#>  3 bad           10 owner      36    46 married yes     freelance       90
#>  4 good           0 rent       60    24 single  no      fixed           63
#>  5 good           0 rent       36    26 single  no      fixed           46
#>  6 good           1 owner      60    36 married no      fixed           75
#>  7 good          29 owner      60    44 married no      fixed           75
#>  8 good           9 parents    12    27 single  no      fixed           35
#>  9 good           0 owner      60    32 married no      freelance       90
#> 10 bad            0 parents    48    41 married no      partime         90
#> # ℹ 4,444 more rows
#> # ℹ 5 more variables: Income <int>, Assets <int>, Debt <int>,
#> #   Amount <int>, Price <int>

# If `new_data` contains values not in `x` at the [prep()] function,
# the [bake()] function will break.
if (FALSE) {
recipe(credit_data %>% dplyr::filter(Home != "rent")) %>%
  check_new_values(Home) %>%
  prep() %>%
  bake(new_data = credit_data)
}

# By default missing values are ignored, so this passes.
recipe(credit_data %>% dplyr::filter(!is.na(Home))) %>%
  check_new_values(Home) %>%
  prep() %>%
  bake(credit_data)
#> # A tibble: 4,454 × 14
#>    Status Seniority Home     Time   Age Marital Records Job       Expenses
#>    <fct>      <int> <fct>   <int> <int> <fct>   <fct>   <fct>        <int>
#>  1 good           9 rent       60    30 married no      freelance       73
#>  2 good          17 rent       60    58 widow   no      fixed           48
#>  3 bad           10 owner      36    46 married yes     freelance       90
#>  4 good           0 rent       60    24 single  no      fixed           63
#>  5 good           0 rent       36    26 single  no      fixed           46
#>  6 good           1 owner      60    36 married no      fixed           75
#>  7 good          29 owner      60    44 married no      fixed           75
#>  8 good           9 parents    12    27 single  no      fixed           35
#>  9 good           0 owner      60    32 married no      freelance       90
#> 10 bad            0 parents    48    41 married no      partime         90
#> # ℹ 4,444 more rows
#> # ℹ 5 more variables: Income <int>, Assets <int>, Debt <int>,
#> #   Amount <int>, Price <int>

# Use `ignore_NA = FALSE` if you consider missing values  as a value,
# that should not occur when not observed in the train set.
if (FALSE) {
recipe(credit_data %>% dplyr::filter(!is.na(Home))) %>%
  check_new_values(Home, ignore_NA = FALSE) %>%
  prep() %>%
  bake(credit_data)
}
源代碼:R/newvalues.R

相關用法


注:本文由純淨天空篩選整理自Max Kuhn等大神的英文原創作品 Check for New Values。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。