使用recipes和caret的preProcess进行预处理的区别

5

作为机器学习流程的一部分,我一直在探索新的recipes变量转换包。由于所有的新扩展,我选择了这种升级方法,不再使用caretpreProcess函数。但我发现这两个包对转换后的数据给出非常不同的结果:

library(caret) # V6.0-79
library(recipes) # V0.1.2
library(MASS) # V7.3-47
# transform variables using recipes
rec_box <- recipe(~ ., data = as.data.frame(state.x77)) %>% 
  step_BoxCox(., everything()) %>% 
  prep(., training = as.data.frame(state.x77)) %>% 
  bake(., as.data.frame(state.x77)) 

> head(rec_box)
# A tibble: 6 x 8
  Population Income Illiteracy `Life Exp` Murder `HS Grad` Frost  Area
       <dbl>  <dbl>      <dbl>      <dbl>  <dbl>     <dbl> <dbl> <dbl>
1       8.19   138.     0.647   60171653.   6.89      651.   20.  56.0
2       5.90   185.     0.376   61218586.   5.52     1632.  152. 106. 
3       7.70   155.     0.527   66409311.   4.08     1253.   15.  69.4
4       7.65   133.     0.570   66885876.   5.05      609.   65.  56.4
5       9.96   165.     0.0936  71570875.   5.13     1445.   20.  75.5
6       7.84   161.    -0.382   73188251.   3.62     1503.  166.  67.7

# transform variables using preProcess
pre_box <- preProcess(x = as.data.frame(state.x77), method = c('BoxCox')) %>% 
  predict(. ,newdata = as.data.frame(state.x77)) 

> head(pre_box)
    # A tibble: 6 x 8
      Population Income Illiteracy `Life Exp` Murder `HS Grad` Frost  Area
           <dbl>  <dbl>      <dbl>      <dbl>  <dbl>     <dbl> <dbl> <dbl>
    1       8.19   118.     0.642       2383.   6.83      618.   20.  38.7
    2       5.90   157.     0.374       2401.   5.47     1538.  152.  65.7
    3       7.70   133.     0.524       2488.   4.05     1183.   15.  46.3
    4       7.65   114.     0.566       2496.   5.01      579.   65.  38.9
    5       9.96   141.     0.0935      2571.   5.09     1363.   20.  49.7
    6       7.84   138.    -0.383       2596.   3.60     1418.  166.  45.4


## Subtract recipe transformations from MARS::boxcox via caret::preProcess
colMeans(rec_box - pre_box)

> colMeans(rec_box - pre_box)
  Population       Income   Illiteracy     Life Exp       Murder      HS Grad        Frost         Area 
0.000000e+00 2.215800e+01 2.515464e-03 6.803437e+07 2.638715e-02 5.883549e+01 0.000000e+00 1.745788e+01

看起来他们在某些列上达成了一致,但其他列的差异非常大。这些转换之间为什么会有如此不同?还有其他人发现类似的差异吗?

1个回答

3

这个差异是由于在 preProcess 函数中对 lambdas 进行了舍入,舍入到一位小数。

看下面的例子:

library(caret) 
library(recipes) 
library(MASS)
library(mlbench)
data(Sonar)

df <- Sonar[,-61]

使用preProcess函数并将fudge设置为0(不容忍对lambda的0/1强制转换),可以实现对IT技术相关内容的处理。
z2 <- preProcess(x = as.data.frame(df), method = c('BoxCox'), fudge = 0)

并使用 配方

z <- recipe(~ ., data = as.data.frame(df )) %>% 
  step_BoxCox(., everything()) %>% 
  prep(., training = as.data.frame(df))

让我们检查与 食谱 相关的 Lambda:

z$steps[[1]]$lambdas
#output
        V1         V2         V3         V4         V5         V6         V7         V8         V9        V10        V11        V12 
0.09296796 0.23383117 0.19487939 0.11471259 0.18688851 0.35852835 0.48787887 0.36830343 0.26340880 0.29810673 0.33913896 0.50361765 
       V13        V14        V15        V16        V17        V18        V19        V20        V21        V22        V23        V24 
0.49178396 0.35997958 0.43900093 0.28981749 0.22843441 0.27016373 0.50573719 0.83436868 1.02366629 1.15194335 1.35062142 1.44484148 
       V25        V26        V27        V28        V29        V30        V31        V32        V33        V34        V35        V36 
1.51851127 1.61365888 1.47445453 1.44448827 1.22132457 1.00145613 0.66343491 0.61951328 0.53028496 0.45278118 0.39019507 0.37536033 
       V37        V38        V39        V40        V41        V42        V52        V53        V54        V55        V56        V57 
0.28428050 0.23439217 0.29554367 0.47263000 0.34455069 0.44036919 0.15240917 0.30314637 0.28647186 0.16202628 0.27153385 0.17005357 
       V58        V59        V60 
0.15688906 0.28761156 0.06652761 

还有 preProcess 的 lambdas:

sapply(z2$bc, function(x) x$lambda)
#output
 V1  V2  V3  V4  V5  V6  V7  V8  V9 V10 V11 V12 V13 V14 V15 V16 V17 V18 V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 
0.1 0.2 0.2 0.1 0.2 0.4 0.5 0.4 0.3 0.3 0.3 0.5 0.5 0.4 0.4 0.3 0.2 0.3 0.5 0.8 1.0 1.2 1.4 1.4 1.5 1.6 1.5 1.4 1.2 1.0 0.7 0.6 0.5 0.5 
V35 V36 V37 V38 V39 V40 V41 V42 V52 V53 V54 V55 V56 V57 V58 V59 V60 
0.4 0.4 0.3 0.2 0.3 0.5 0.3 0.4 0.2 0.3 0.3 0.2 0.3 0.2 0.2 0.3 0.1 

所以:
df$V1^z$steps[[1]]$lambdas[1]

不等于

df$V1^sapply(z2$bc, function(x) x$lambda)[1]

使用默认的fudge = 0.2,由于-0.2 - 0.2将被更改为0(即log转换),而0.8 - 1.2的lambda将被更改为1(没有转换),因此差异会更大。
我不会担心这些差异,两个函数都会减少数据的偏斜。只是不要将它们混合在同一个训练管道中。
此外,为了获得更加无偏的性能估计,这些转换应该在重新采样期间进行,而不是在之前,以避免数据泄漏。

这个例子非常有趣的地方在于它不仅限于BoxCox转换,我发现几乎每一个recipepreProcess的比较都是如此。我想知道在caret包中应用了什么通用规则 - 如果存在任意舍入规则,那将会非常令人沮丧,因为你从哪里开始查找呢?在我的实际工作流程中,我使用了8种不同的转换方法,就性能而言,使用preProcess的结果比recipe转换要好得多,测试ROC也更好。 - Hanjo Odendaal
在我看来,它是这样的:round(x, digits = 1)。测试ROC提高了多少?如果您使用另一个测试集,也许“recipe”的lambda值会表现更好,这可能只是随机改进。 - missuse
使用preProcess的方法BoxCoxYeoJohnsonspatialSign,并且没有进行数据转换,我得到了0.88-0.92的ROC值(平均改进了0.03个单位)。而使用recipes进行所有转换后的数据集都只有约0.55的ROC值,而原始数据则给出了约0.88的ROC值。显然这是我的用例(caret::data(segmentationData)),但看起来差距太大了。 - Hanjo Odendaal
一定还有其他问题。如果您希望,可以发布另一个问题,以可重现的方式描述问题,稍后我会进行调查。 - missuse
让我仔细查看这百万行代码。如果我能够复现结果的差异,那么我会发布一个新问题的链接。否则,我们可以认为这个问题已经关闭了。 - Hanjo Odendaal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接