2013-07-14 4 views
1

저는 gbm 모델에 맞게 캐럿을 사용하고 있습니다. trainedGBM$finalModel$fit으로 전화 할 때 출력이 올바르게 표시됩니다.R gbm 모델 예측이 모델 적합성과 일치하지 않는 이유는 무엇입니까?

그러나 predict(trainedGBM$finalModel, origData, type="response")을 호출하면 매우 다른 결과를 얻고 predict(trainedGBM$finalModel, type="response")은 origData가 첨부되어 있어도 여전히 다른 결과를 산출합니다. 내 생각에, 이러한 호출은 동일한 결과를 가져와야합니다. 누군가가 나를 식별하도록 도울 수 있습니까? 하여 gbmGrid

y finalModelFit predictDataSpec predictNoDataSpec 
9000  6138.8920  2387.182   2645.993 
5000  3850.8817  2767.990   2467.157 
3000  3533.1183  2753.551   2044.578 
2500  1362.9802  2672.484   1972.361 
1500  5080.2112  2449.185   2000.568 
750  2284.8188  2728.829   2063.829 
1500  2672.0146  2359.566   2344.451 
5000  3340.5828  2435.137   2093.939 
    0  1303.9898  2377.770   2041.871 
500  879.9798  2691.886   2034.307 
3000  2928.4573  2327.627   1908.876 
+0

이것이 '캐럿 (caret)'패키지에 있다고 맞습니까? 당신이해야 할 일이'라이브러리 (_whatever_package_train_came_from)'에 넣어 졌을 때 사람들이 이런 종류의 질문을하게하는 것은 정말로 무의미하다. –

+0

또 하나의 별개의 whinge로 :'attach '를 사용하는 것은 이해하기 힘든 공통 소스이다. 오류. 물론 "origData"에 대해 더 자세히 설명해야합니다. –

+0

여기에 어떤 종류의 데이터 설명이 유용합니까? 필자는 약 7000 개의 레코드를 가지고 있는데, y는 26 개의 기능, 둘 다 요소 및 숫자의 함수로 적합합니다. – Nostradamus

답변

7

기반이 상호 작용의 깊이 (14)와 (20) 사이에서 변화하며, 나무의 수축 및 수가 고정되어

library(caret) 
library(gbm) 

attach(origData) 
gbmGrid <- expand.grid(.n.trees = c(2000), 
         .interaction.depth = c(14:20), 
         .shrinkage = c(0.005)) 
trainedGBM <- train(y ~ ., method = "gbm", distribution = "gaussian", 
        data = origData, tuneGrid = gbmGrid, 
        trControl = trainControl(method = "repeatedcv", number = 10, 
              repeats = 3, verboseIter = FALSE, 
              returnResamp = "all")) 
ntrees <- gbm.perf(trainedGBM$finalModel, method="OOB") 
data.frame(y, 
      finalModelFit = trainedGBM$finalModel$fit, 
      predictDataSpec = predict(trainedGBM$finalModel, origData, type="response", n.trees=ntrees), 
      predictNoDataSpec = predict(trainedGBM$finalModel, type="response", n.trees=ntrees)) 

상기 코드는 다음의 부분적인 결과를 얻을 0.005, 2000으로 각각 나타났다. TrainedGBM은 현재와 같은 최적의 상호 작용 수준만을 찾도록 설계되었습니다. 최적 수준의 상호 작용이 14에서 20 사이 인 경우 OOB 기준에 따라 최적의 트리 수는 얼마입니까? 이 gbm.perf에서 계산됩니다. 예측은 모델의 트리 수에 따라 다르므로 훈련 된 GBM을 기반으로 한 예측은 ntrees = 2000이고 예측은 gbm.perf을 기반으로하며 해당 함수에서 추정 한 최적 수는 ntrees입니다. 이는 trainedGBM$finalModel$fitpredict(trainedGBM$finalModel, type="response", n.trees=ntrees)의 차이를 설명합니다.

홍채 데이터를 기반으로 예를 표시하려면 최적의 상호 작용을 깊이 조건으로 나무의 최적의 수를 찾기

# Resampling results across tuning parameters: 

# interaction.depth Accuracy Kappa Accuracy SD Kappa SD 
# 1     0.947  0.92 0.0407  0.061 
# 2     0.947  0.92 0.0407  0.061 
# 3     0.944  0.917 0.0432  0.0648 
# 4     0.944  0.917 0.0395  0.0592 

# Tuning parameter 'n.trees' was held constant at a value of 100 
# Tuning parameter 'shrinkage' was held constant at a value of 0.05 
# Accuracy was used to select the optimal model using the largest value. 
# The final values used for the model were interaction.depth = 1, n.trees = 100 
# and shrinkage = 0.05.  

을주고 분류보다는 회귀 모델

library(caret) 
library(gbm) 

set.seed(42) 

gbmGrid <- expand.grid(.n.trees = 100, 
        .interaction.depth = 1:4, 
        .shrinkage = 0.05) 


trainedGBM <- train(Species ~ ., method = "gbm", distribution='multinomial', 
       data = iris, tuneGrid = gbmGrid, 
       trControl = trainControl(method = "repeatedcv", number = 10, 
             repeats = 3, verboseIter = FALSE, 
             returnResamp = "all")) 
print(trainedGBM)   

으로 GBM를 사용하여 설정 :

ntrees <- gbm.perf(trainedGBM$finalModel, method="OOB") 
# Giving ntrees = 50 

나무 수를 변경하여 모델을 훈련 시키면 및 상호 작용 깊이 : 나무의 최적의 수는 우리가 나무와 상호 작용 깊이의 번호가 모두 다를 수 있다는

gbmGrid2 <- expand.grid(.n.trees = 1:100, 
        .interaction.depth = 1:4, 
        .shrinkage = 0.05) 

trainedGBM2 <- train(Species ~ ., method = "gbm", 
       data = iris, tuneGrid = gbmGrid2, 
       trControl = trainControl(method = "repeatedcv", number = 10, 
             repeats = 3, verboseIter = FALSE, 
             returnResamp = "all")) 

print(trainedGBM2) 

# Tuning parameter 'shrinkage' was held constant at a value of 0.05 
# Accuracy was used to select the optimal model using the largest value. 
# The final values used for the model were interaction.depth = 2, n.trees = 39 
# and shrinkage = 0.05. 

gbm.perf에 대해 계산이 상당히 가깝습니다.

관련 문제