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

我在探索新的recipes包,用于机器学习管道中的变量变换。我选择了这种方法——从使用caretpreProcess函数升级,因为有许多新的扩展。但是我发现这两个包对变换后的数据给出了非常不同的结果:

library(caret) # V6.0-79library(recipes) # V0.1.2library(MASS) # V7.3-47# 使用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.02       5.90   185.     0.376   61218586.   5.52     1632.  152. 106. 3       7.70   155.     0.527   66409311.   4.08     1253.   15.  69.44       7.65   133.     0.570   66885876.   5.05      609.   65.  56.45       9.96   165.     0.0936  71570875.   5.13     1445.   20.  75.56       7.84   161.    -0.382   73188251.   3.62     1503.  166.  67.7# 使用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## 从MARS::boxcox via caret::preProcess中减去recipes变换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

所以看起来在一些列上它们是一致的,但在其他列上差异很大。为什么这些变换会有这么大的不同?其他人是否也发现了类似的差异?


回答:

这种差异是由于preProcess函数中lambdas的四舍五入,这些值被四舍五入到小数点后一位。

请看这个例子:

library(caret) library(recipes) library(MASS)library(mlbench)data(Sonar)df <- Sonar[,-61]

使用preProcess函数并将fudge设置为0(对lambdas的0/1强制转换没有容忍度)。

z2 <- preProcess(x = as.data.frame(df), method = c('BoxCox'), fudge = 0)

以及使用recepies

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

让我们检查recepies的lambdas:

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 - 02将被更改为0,即log变换,而0.8 - 1.2的lambdas将被更改为1——即无变换。

我不会担心这些差异,这两个函数都会减少数据的偏斜。只是不要在同一个训练管道中混合使用它们。

此外,为了获得更无偏见的性能估计,这些变换应该在重新采样期间进行,而不是在之前进行,以避免数据泄漏。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注