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


R dials grid_regular 創建調整參數網格


可以為任意數量的參數對象創建隨機和規則網格。

用法

grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

# S3 method for parameters
grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

# S3 method for list
grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

# S3 method for param
grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

# S3 method for workflow
grid_regular(x, ..., levels = 3, original = TRUE, filter = NULL)

grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

# S3 method for parameters
grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

# S3 method for list
grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

# S3 method for param
grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

# S3 method for workflow
grid_random(x, ..., size = 5, original = TRUE, filter = NULL)

參數

x

param 對象、列表或 parameters

...

一個或多個 param 對象(例如 mtry()penalty() )。所有對象都不能在參數範圍或值中具有 unknown() 值。

levels

用於製作規則網格的每個參數值的數量的整數。 levels 可以是單個整數或整數向量,其長度與 ... 中的參數數量相同。 levels 可以是命名整數向量,其名稱與參數的 id 值匹配。

original

邏輯:參數應該采用原始單位還是變換後的空間(如果有)?

filter

邏輯:是否應該在生成網格之前過濾參數。必須是引用參數名稱且計算結果為邏輯向量的單個表達式。

size

為隨機網格返回的參數值組合總數的單個整數。如果從此大小生成重複組合,則返回較小的唯一集合。

一點點。每個參數都有列,每個參數組合都有一行。

細節

請注意,根據函數的調用方式,網格可能會有所不同。如果調用直接使用參數對象,則可能的範圍來自 dials 中的對象。例如:

mixture()
## Proportion of Lasso Penalty (quantitative)
## Range: [0, 1]
set.seed(283)
mix_grid_1 <- grid_random(mixture(), size = 1000)
range(mix_grid_1$mixture)
## [1] 0.001490161 0.999741096

但是,在某些情況下,parsniprecipe 包會覆蓋特定模型和預處理步驟的默認範圍。如果網格函數使用從模型或配方創建的 parameters 對象,則範圍可能具有不同的默認值(特定於這些模型)。使用上麵的示例,上麵的 mixture 參數對於 glmnet 模型是不同的:

library(parsnip)
library(tune)

# When used with glmnet, the range is [0.05, 1.00]
glmn_mod <-
  linear_reg(mixture = tune()) %>%
  set_engine("glmnet")

set.seed(283)
mix_grid_2 <- grid_random(extract_parameter_set_dials(glmn_mod), size = 1000)
range(mix_grid_2$mixture)
## [1] 0.05141565 0.99975404

例子

# filter arg will allow you to filter subsequent grid data frame based on some condition.
p <- parameters(penalty(), mixture())
grid_regular(p)
#> # A tibble: 9 × 2
#>        penalty mixture
#>          <dbl>   <dbl>
#> 1 0.0000000001     0  
#> 2 0.00001          0  
#> 3 1                0  
#> 4 0.0000000001     0.5
#> 5 0.00001          0.5
#> 6 1                0.5
#> 7 0.0000000001     1  
#> 8 0.00001          1  
#> 9 1                1  
grid_regular(p, filter = penalty <= .01)
#> # A tibble: 6 × 2
#>        penalty mixture
#>          <dbl>   <dbl>
#> 1 0.0000000001     0  
#> 2 0.00001          0  
#> 3 0.0000000001     0.5
#> 4 0.00001          0.5
#> 5 0.0000000001     1  
#> 6 0.00001          1  

# Will fail due to unknowns:
# grid_regular(mtry(), min_n())

grid_regular(penalty(), mixture())
#> # A tibble: 9 × 2
#>        penalty mixture
#>          <dbl>   <dbl>
#> 1 0.0000000001     0  
#> 2 0.00001          0  
#> 3 1                0  
#> 4 0.0000000001     0.5
#> 5 0.00001          0.5
#> 6 1                0.5
#> 7 0.0000000001     1  
#> 8 0.00001          1  
#> 9 1                1  
grid_regular(penalty(), mixture(), levels = 3:4)
#> # A tibble: 12 × 2
#>         penalty mixture
#>           <dbl>   <dbl>
#>  1 0.0000000001   0    
#>  2 0.00001        0    
#>  3 1              0    
#>  4 0.0000000001   0.333
#>  5 0.00001        0.333
#>  6 1              0.333
#>  7 0.0000000001   0.667
#>  8 0.00001        0.667
#>  9 1              0.667
#> 10 0.0000000001   1    
#> 11 0.00001        1    
#> 12 1              1    
grid_regular(penalty(), mixture(), levels = c(mixture = 4, penalty = 3))
#> # A tibble: 12 × 2
#>         penalty mixture
#>           <dbl>   <dbl>
#>  1 0.0000000001   0    
#>  2 0.00001        0    
#>  3 1              0    
#>  4 0.0000000001   0.333
#>  5 0.00001        0.333
#>  6 1              0.333
#>  7 0.0000000001   0.667
#>  8 0.00001        0.667
#>  9 1              0.667
#> 10 0.0000000001   1    
#> 11 0.00001        1    
#> 12 1              1    
grid_random(penalty(), mixture())
#> # A tibble: 5 × 2
#>         penalty mixture
#>           <dbl>   <dbl>
#> 1 0.00000000433   0.312
#> 2 0.0000000435    0.174
#> 3 0.00359         0.423
#> 4 0.00000299      0.192
#> 5 0.00000146      0.633

源代碼:R/grids.R

相關用法


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