2016-09-22 129 views
1

我已經用​​方法訓練了一個數據集。例如:在R(插圖)中繪製決策樹

ctrl <- trainControl(
        method = "LGOCV", 
        repeats = 3, 
        savePred=TRUE, 
        verboseIter = TRUE, 
        preProcOptions = list(thresh = 0.95) 
        ) 

preProcessInTrain<-c("center", "scale") 
metric_used<-"Accuracy" 
model <- train(
       Output ~ ., data = training, 
       method = "rf", 
       trControl = ctrl, 
       metric=metric_used, 
       tuneLength = 10, 
       preProc = preProcessInTrain 
      ) 

thath後,我要繪製的decission樹,但是當我WIRTE plot(model),我得到這個:plot(model)

如果我寫plot(model$finalModel),我得到這個:plot(model$finalModel)

我想繪製decission樹...

我怎麼能這樣做? 謝謝:)

回答

2

您使用的模型是隨機森林,它不是一個決策樹,而是大量樹木的集合。繪製最終模型將繪製樹數增加的訓練和測試數據集的錯誤率,如下所示。

enter image description here

如果你想要一個決策樹相反,你可能想訓練CART模型如下所示:

model <- train(
    Species ~ ., data = training, 
    method = "rpart", 
    trControl = ctrl, 
    metric=metric_used, 
    tuneLength = 10, 
    preProc = preProcessInTrain 
) 
library(rpart.plot) 
rpart.plot(model$finalModel) 

現在繪製如上最終的模型將繪製決策樹您。

+1

非常感謝!我有一個問題。什麼意思是藍色,綠色,黑色和紅色線?我的意思是,它們之間有什麼不同? –

+1

感謝您的提問。黑線表示整體OOB錯誤,而其他三條彩色線表示訓練數據中3個類別中的每個類別的OOB錯誤(我使用具有3個不同類別標籤的訓練數據)。 –