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


R recipes step_percentile 百分位變換

step_percentile() 創建配方步驟的規範,用訓練集中的百分位數替換變量的值。

用法

step_percentile(
  recipe,
  ...,
  role = NA,
  trained = FALSE,
  ref_dist = NULL,
  options = list(probs = (0:100)/100),
  outside = "none",
  skip = FALSE,
  id = rand_id("percentile")
)

參數

recipe

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

...

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

role

對於此步驟創建的模型項,應為其分配什麽分析角色?默認情況下,此步驟根據原始變量創建的新列將用作模型中的預測變量。

trained

指示預處理數量是否已估計的邏輯。

ref_dist

一旦 prep() 訓練了該預處理步驟,計算出的百分位數就會存儲在此處。

options

要傳遞給 stats::quantile() 的命名選項列表。請參閱詳細信息了解更多信息。

outside

一個字符,說明如何在區間 [min(x), max(x)] 之外進行插值。 none 意味著什麽都不會發生,超出範圍的值將為 NAlower 意味著小於 min(x) 的新值將被賦予值 0upper 意味著大於 max(x) 的新值將被賦予值 1both 將處理這兩種情況。默認為 none

skip

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

id

該步驟特有的字符串,用於標識它。

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

箱重

此步驟執行可以利用案例權重的無監督操作。因此,個案權重僅與頻率權重一起使用。有關更多信息,請參閱 case_weights 中的文檔和 tidymodels.org 中的示例。

也可以看看

例子

data(biomass, package = "modeldata")

biomass_tr <- biomass[biomass$dataset == "Training", ]
biomass_te <- biomass[biomass$dataset == "Testing", ]

rec <- recipe(
  HHV ~ carbon + hydrogen + oxygen + nitrogen + sulfur,
  data = biomass_tr
) %>%
  step_percentile(carbon)

prepped_rec <- prep(rec)

prepped_rec %>%
  bake(biomass_te)
#> # A tibble: 80 × 6
#>    carbon hydrogen oxygen nitrogen sulfur   HHV
#>     <dbl>    <dbl>  <dbl>    <dbl>  <dbl> <dbl>
#>  1 0.421      5.67   47.2     0.3    0.22  18.3
#>  2 0.18       5.5    48.1     2.85   0.34  17.6
#>  3 0.156      5.5    49.1     2.4    0.3   17.2
#>  4 0.423      6.1    37.3     1.8    0.5   18.9
#>  5 0.666      6.32   42.8     0.2    0     20.5
#>  6 0.218      5.5    41.7     0.7    0.2   18.5
#>  7 0.0803     5.23   54.1     1.19   0.51  15.1
#>  8 0.139      4.66   33.8     0.95   0.2   16.2
#>  9 0.0226     4.4    31.1     0.14   4.9   11.1
#> 10 0.0178     3.77   23.7     4.63   1.05  10.8
#> # ℹ 70 more rows

tidy(rec, 1)
#> # A tibble: 1 × 4
#>   terms  value percentile id              
#>   <chr>  <dbl>      <dbl> <chr>           
#> 1 carbon    NA         NA percentile_dwNDP
tidy(prepped_rec, 1)
#> # A tibble: 101 × 4
#>    term   value percentile id              
#>    <chr>  <dbl>      <dbl> <chr>           
#>  1 carbon  14.6          0 percentile_dwNDP
#>  2 carbon  25.9          1 percentile_dwNDP
#>  3 carbon  28.4          2 percentile_dwNDP
#>  4 carbon  31.6          3 percentile_dwNDP
#>  5 carbon  35.1          4 percentile_dwNDP
#>  6 carbon  35.9          5 percentile_dwNDP
#>  7 carbon  37.5          6 percentile_dwNDP
#>  8 carbon  38.3          7 percentile_dwNDP
#>  9 carbon  38.9          8 percentile_dwNDP
#> 10 carbon  39.6          9 percentile_dwNDP
#> # ℹ 91 more rows
源代碼:R/percentile.R

相關用法


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