【问题标题】:Does extractPrediction() support factors?extractPrediction() 是否支持因子?
【发布时间】:2015-06-12 00:00:38
【问题描述】:

我正在尝试使用随机森林模型作为我正在测试的几个模型之一,包括神经网络(nnetneuralnet),所有这些模型都使用方便的 caret 包。随机森林模型支持因子的使用,所以对于这个模型,与其使用dummyVars() 将因子转换为数值对比,我想我只是将它们保留为因子。这在训练步骤中效果很好(train()):

library(caret)

#Set dependent
seed = 123
y = "Sepal.Length"

#Partition (iris) data into train and test sets
set.seed(seed)
train.idx = createDataPartition(y = iris[,y], p = .8, list = FALSE)
train.set = iris[train.idx,]
test.set = iris[-train.idx,]

train.set = data.frame(train.set)
test.set = data.frame(test.set)

#Select features
features = c("Sepal.Width", "Petal.Length", "Petal.Width", "Species")
mod.features = paste(features, collapse = " + ")

#Create formula
mod.formula = as.formula(paste(y, mod.features, sep = " ~ "))

#Train model
mod <- train(mod.formula, data = train.set,
             method = "rf")

但是当我尝试使用extractPrediction() 时,它失败了:

#Test model with extractPrediction()
testPred = extractPrediction(models = list(mod),
                             testX = test.set[,features],
                             testY = test.set[,y])

predict.randomForest(modelFit, newdata) 中的错误: newdata 中缺少训练数据

现在,据我所知,这是因为在调用 train() 期间,为因子创建了 1-hot 编码/对比,因此创建了一些新的变量名称。即使有因素,基本 predict() 方法似乎也能正常工作:

#Test model with predict()
testPred = predict(mod$finalModel, 
                   newData = test.set[, features])

当我使用 dummyVars() 将因子转换为数值对比时,extractPrediction() 工作正常:

#Train and test model using dummyVar
data.dummies = dummyVars(~.,data = iris)
data = predict(data.dummies, newdata = iris)

set.seed(seed)
train.idx = createDataPartition(y = data[,y], p = .8, list = FALSE)
train.set = data[train.idx,]
test.set = data[-train.idx,]

features = c("Sepal.Width", "Petal.Length", "Petal.Width", "Species.setosa",
             "Species.versicolor", "Species.virginica")
mod.features = paste(features, collapse = " + ")

#Create formula
mod.formula = as.formula(paste(y, mod.features, sep = " ~ "))

train.set = data.frame(train.set)
test.set = data.frame(test.set)

mod <- train(mod.formula, data = train.set,
             method = "rf")

testPred = extractPrediction(models = list(mod),
                             testX = test.set[,features],
                             testY = test.set[,y])

谁能向我解释这是为什么?让extractPrediction() 与我的多模型测试管道中使用的因素一起工作会很棒。我想我可以在开始时使用dummyVars() 转换所有内容,但我很想知道为什么extractPrediction() 在这种情况下不能处理因子,即使predict() 确实有效。

【问题讨论】:

    标签: r r-factor r-caret


    【解决方案1】:

    如果你使用默认的函数接口而不是使用公式的接口,你应该是在做生意。

    set.seed(1234)
    mod_formula <- train(
        Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species
      , data = iris
      , method = "rf")
    
    test_formula <- extractPrediction(
        models = list(mod_formula)
    )
    
    set.seed(1234)
    mod_default <- train(
        y = iris$Sepal.Length
      , x = iris[, c('Sepal.Width', 'Petal.Length', 'Petal.Width', 'Species')]
      , method = "rf")
    
    test_default <- extractPrediction(
      models = list(mod_default)
    )
    

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多