首页 文章

如何在插入符号train()中仅对某些变量应用预处理?

提问于
浏览
1

我想在列车功能中使用插入符号的超级方便的预处理方式,以便为后面的预测提供相同的操作 . 但是,我想将预处理仅应用于数字列的 some . 我怎么指定这个?我可以以某种方式使用trainControl中的preProcOptions参数吗?

如果我使用普通的preProcess对象,我可以这样做:

preObj <- preProcess(training[,"SomeCol"], method=c("scale"))
preData <- predict(preObj, training[,"SomeCol"])

但我不知道如何用train()和trainControl()实现相同的目标:

ctrl <- trainControl(method="repeatedcv",repeats = 1, preProcOptions = list(x=x[,"SomeCol"]))
fit <- train(y ~ ., data = training, method = "rf", trControl = ctrl, preProcess=c("scale"))

1 回答

  • 2

    我不愿意提到这一点,但是你可以做到这一点,并且有一种无证的方式 . [2879371_

    > pp <- preProcess(iris, method = list(center = "Petal.Width", scale = names(iris)[1:2]))
    > pp
    Created from 150 samples and 4 variables
    
    Pre-processing:
      - centered (1)
      - ignored (1)
      - scaled (2)
    
    > predict(pp, head(iris))
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    1     6.158928    8.029986          1.4  -0.9993333  setosa
    2     5.917402    6.882845          1.4  -0.9993333  setosa
    3     5.675875    7.341701          1.3  -0.9993333  setosa
    4     5.555112    7.112273          1.5  -0.9993333  setosa
    5     6.038165    8.259414          1.4  -0.9993333  setosa
    6     6.521218    8.947698          1.7  -0.7993333  setosa
    > head(iris)
    Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    1          5.1         3.5          1.4         0.2  setosa
    2          4.9         3.0          1.4         0.2  setosa
    3          4.7         3.2          1.3         0.2  setosa
    4          4.6         3.1          1.5         0.2  setosa
    5          5.0         3.6          1.4         0.2  setosa
    6          5.4         3.9          1.7         0.4  setosa
    

    我还没有测试所有方法组合的边缘情况,所以如果你选择使用它,请做一些测试 .

相关问题