gpt4 book ai didi

R-Caret:如何使用多个模型构建更高效的模型并预测新结果

转载 作者:行者123 更新时间:2023-11-30 08:32:03 46 4
gpt4 key购买 nike

我的训练数据集 (train) 是一个具有 n 个特征 的数据框架,以及一个具有结果 y 的附加列。我建立了3个单独的模型,例如:

m1 <- train(y ~ ., data = train, method = "lda")
m2 <- train(y ~ ., data = train, method = "rf")
m3 <- train(y ~ ., data = train, method = "gbm")

通过测试数据集(测试),我可以评估这些个体模型的质量(当然,它的结果是y):

pred1 <- predict(m1, newdata = test)
pred2 <- predict(m2, newdata = test)
pred3 <- predict(m3, newdata = test)

如果我将每个单独的模型应用到具有 5 个示例的数据帧DATA_TO_PREDICT(结果未知)中,那么输出自然是每个单独模型的 5 个预测:

predict(m1, DATA_TO_PREDICT)
predict(m2, DATA_TO_PREDICT)
predict(m3, DATA_TO_PREDICT)

现在我想使用 R-Caret-Package 与随机森林的组合模型:

DF <- data.frame(pred1, pred2, pred3, y = test$y)
MODEL <- train(y ~ ., data = DF, method = "rf")

我可以观察到组合模型的准确度有所提高:

predMODEL <- predict(MODEL, DF)

但是,如果我在 DATA_TO_PREDICT 中应用组合模型(结果未知),输出不仅有 5 个预测,而且是一个包含重复结果且超过 100 个的巨大列表。我用过:

predict(MODEL, newdata = DATA_TO_PREDICT)

示例:

这里我展示一个输出错误的具体例子。也就是说,我想预测 4 个新数据,但我得到的结果有几十个输出:

library(caret)
library(gbm)
set.seed(10)
library(AppliedPredictiveModeling)
data(AlzheimerDisease)
adData = data.frame(diagnosis,predictors)
inTrain = createDataPartition(adData$diagnosis, p = 3/4)[[1]]
training = adData[ inTrain,]
testing = adData[-inTrain,]

inTEST <- (5:nrow(testing))
test <- testing[inTEST,]
DATA_TO_PREDICT <- testing[-inTEST,]

m1 <- train(diagnosis ~ ., data=training, method="rf")
m2 <- train(diagnosis ~ ., data=training, method="gbm")
m3 <- train(diagnosis ~ ., data=training, method="lda")
p1 <- predict(m1, newdata = test)
p2 <- predict(m2, newdata = test)
p3 <- predict(m3, newdata = test)

DF <- data.frame(p1, p2, p3, diagnosis = test$diagnosis)
MODEL <- train(diagnosis ~ ., data = DF, method = "rf")
predMODEL <- predict(MODEL, DF)

那么如果我构建组合模型:

pred1 <- predict(m1, DATA_TO_PREDICT)
pred2 <- predict(m2, DATA_TO_PREDICT)
pred3 <- predict(m3, DATA_TO_PREDICT)
DF2 <- data.frame(pred1, pred2, pred3)
predict(MODEL, newdata = DF2)

请注意,DATA_TO_PREDICT 只有 4 个示例,输出为:

  [1] Control Control Control Control Control Control Control Control
[9] Control Control Control Control Control Control Control Control
[17] Control Control Control Control Control Control Control Control
[25] Control Control Control Control Control Control Control Control
[33] Control Control Control Control Control Control Control Control
[41] Control Control Control Control Control Control Control Control
[49] Control Control Control Control Control Control Control Control
[57] Control Control Control Control Control Control Control Control
[65] Control Control Control Control Control Control Control Control
[73] Control Control Control Control Control Control
Levels: Impaired Control

最佳答案

这是因为 MODEL 是根据三个单独模型(pred1pred2pred3 的预测进行训练的> 用于测试数据),并且在最后一步中,DATA_TO_PREDICT 被提供给 MODEL,而后者由观察结果组成。首先,必须存储 DATA_TO_PREDICT 各个模型的预测值,然后将其用作 MODELnewdata

# (Beginning of the example omitted)
DF <- data.frame(p1, p2, p3, diagnosis = test$diagnosis)
# This trains a model with predictions as inputs:
MODEL <- train(diagnosis ~ ., data = DF, method = "rf")

# This is missing ----------------------
# To get the inputs for the ensemble model
# the predictions for DATA_TO_PREDICT are needed
p1b <- predict(m1, newdata = DATA_TO_PREDICT)
p2b <- predict(m2, newdata = DATA_TO_PREDICT)
p3b <- predict(m3, newdata = DATA_TO_PREDICT)
DFb <- data.frame(p1b, p2b, p3b)
colnames(DFb) <- c("p1", "p2", "p3")
#----------------------------------------

predMODEL <- predict(MODEL, DFb)
# [1] Control Control Control Control

关于R-Caret:如何使用多个模型构建更高效的模型并预测新结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29143320/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com