GBM软件包与使用GBM的插入符


13

我一直在使用进行模型调整caret,但随后使用该gbm软件包重新运行模型。据我了解,caret程序包使用gbm的输出应相同。然而,data(iris)使用RMSE和R ^ 2作为评估指标,使用进行的快速测试显示模型中的差异约为5%。我想使用来找到最佳模型性能,caret但要重新运行gbm以利用部分依赖图。下面的代码具有可重复性。

我的问题是:

1)为什么即使这两个软件包应该相同,我仍会看到这两个软件包之间的差异(我知道它们是随机的,但5%的差异还是很大的,尤其是当我没有使用iris建模时使用的很好的数据集时) 。

2)同时使用这两个软件包有什么优点或缺点?

3)不相关:使用iris数据集时,最佳interaction.depth值为5,但高于我所阅读的最大值,使用最大值floor(sqrt(ncol(iris)))为2。这是严格的经验法则还是非常灵活?

library(caret)
library(gbm)
library(hydroGOF)
library(Metrics)
data(iris)

# Using caret
caretGrid <- expand.grid(interaction.depth=c(1, 3, 5), n.trees = (0:50)*50,
                   shrinkage=c(0.01, 0.001),
                   n.minobsinnode=10)
metric <- "RMSE"
trainControl <- trainControl(method="cv", number=10)

set.seed(99)
gbm.caret <- train(Sepal.Length ~ ., data=iris, distribution="gaussian", method="gbm",
              trControl=trainControl, verbose=FALSE, 
              tuneGrid=caretGrid, metric=metric, bag.fraction=0.75)                  

print(gbm.caret)
# caret determines the optimal model to be at n.tress=700, interaction.depth=5, shrinkage=0.01
# and n.minobsinnode=10
# RMSE = 0.3247354
# R^2 = 0.8604

# Using GBM
set.seed(99)
gbm.gbm <- gbm(Sepal.Length ~ ., data=iris, distribution="gaussian", n.trees=700, interaction.depth=5,
           n.minobsinnode=10, shrinkage=0.01, bag.fraction=0.75, cv.folds=10, verbose=FALSE)
best.iter <- gbm.perf(gbm.gbm, method="cv")
print(best.iter)
# Here the optimal n.trees = 540

train.predict <- predict.gbm(object=gbm.gbm, newdata=iris, 700)

print(rmse(iris$Sepal.Length, train.predict))
# RMSE = 0.2377

R2 <- cor(gbm.gbm$fit, iris$Sepal.Length)^2
print(R2)
# R^2 = 0.9178`

Answers:


6

与默认网格一起使用以优化参数并使用预测具有相同的结果:

R2.caret-R2.gbm = 0.0009125435

rmse.caret-rmse.gbm = -0.001680319

library(caret)
library(gbm)
library(hydroGOF)
library(Metrics)
data(iris)

# Using caret with the default grid to optimize tune parameters automatically
# GBM Tuning parameters:
# n.trees (# Boosting Iterations)
# interaction.depth (Max Tree Depth)
# shrinkage (Shrinkage)
# n.minobsinnode (Min. Terminal Node Size)

metric <- "RMSE"
trainControl <- trainControl(method="cv", number=10)

set.seed(99)
gbm.caret <- train(Sepal.Length ~ .
                   , data=iris
                   , distribution="gaussian"
                   , method="gbm"
                   , trControl=trainControl
                   , verbose=FALSE
                   #, tuneGrid=caretGrid
                   , metric=metric
                   , bag.fraction=0.75
                   )                  

print(gbm.caret)

caret.predict <- predict(gbm.caret, newdata=iris, type="raw")

rmse.caret<-rmse(iris$Sepal.Length, caret.predict)
print(rmse.caret)

R2.caret <- cor(gbm.caret$finalModel$fit, iris$Sepal.Length)^2
print(R2.caret)

#using gbm without caret with the same parameters
set.seed(99)
gbm.gbm <- gbm(Sepal.Length ~ .
               , data=iris
               , distribution="gaussian"
               , n.trees=150
               , interaction.depth=3
               , n.minobsinnode=10
               , shrinkage=0.1
               , bag.fraction=0.75
               , cv.folds=10
               , verbose=FALSE
               )
best.iter <- gbm.perf(gbm.gbm, method="cv")
print(best.iter)

train.predict <- predict.gbm(object=gbm.gbm, newdata=iris, 150)

rmse.gbm<-rmse(iris$Sepal.Length, train.predict)
print(rmse.gbm)

R2.gbm <- cor(gbm.gbm$fit, iris$Sepal.Length)^2
print(R2.gbm)

print(R2.caret-R2.gbm)
print(rmse.caret-rmse.gbm)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.