2014-09-23 57 views
0

我正在嘗試在下面創建ROC曲線。我得到一個錯誤,指出預測中的錯誤(bc_rf_predict_prob,bc_test $ Class): 對於預測和標籤,交叉驗證運行的次數必須相等。randomForest中的ROC曲線錯誤

library(mlbench) #has the Breast Cancer dataset in it 
library(caret) 
data(BreastCancer) #two class model 

bc_changed<-BreastCancer[2:11] #removes variables not to be used 


#Create train and test/holdout samples (works fine) 
set.seed(59) 
bc_rand <- bc_changed[order(runif(699)), ] #699 observations 
bc_rand <- sample(1:699, 499) 
bc_train <- bc_changed[ bc_rand,] 
bc_test <- bc_changed[-bc_rand,] 

#random forest decision tree (works fine) 
library(caret) 
library(randomForest) 
set.seed(59) 
bc_rf <- randomForest(Class ~.,data=bc_train, ntree=500,na.action = na.omit, importance=TRUE) 

#ROC 
library(ROCR) 
actual <- bc_test$Class 
bc_rf_predict_prob<-predict(bc_rf, type="prob", bc_test) 
bc.pred = prediction(bc_rf_predict_prob,bc_test$Class) #not work- error 

錯誤 - 錯誤預測(bc_rf_predict_prob,bc_test $類): 交叉驗證運行次數必須相等預測和標籤。

我認爲這是一個事實來當我做:

bc_rf_predict_prob<-predict(bc_rf, type="prob", bc_test) 

我得到一個矩陣,與兩列良性及其概率的名單和惡性及其列表中的第二列的結果的概率。我的邏輯告訴我,我應該只有一個概率向量。

+0

您是否嘗試過使用'str(predict(bc_rf,type =「prob」,bc_test))'來確定'predict'的數據類型/結構? – 2014-09-23 18:24:14

+0

同意這是問題可能出現的地方。我得到矩陣[1:200,1:2] 1 0.12 1 1 0 1 1 1 0.05 1 ... - attr(*,「dimnames」)= 2 .. $:chr [1:200] 「3」「4」「5」「12」... .. $:chr [1:2]「良性」「惡性」 - attr(*,「class」)= chr [1:2]「矩陣「」票「 – mpg 2014-09-23 18:38:53

+0

那麼,我在上面做了什麼? – mpg 2014-09-23 18:39:29

回答

1

根據page 9 of the ROCR Library documentationprediction函數有兩個必需的輸入,predictionslabels,它們必須具有相同的尺寸。

對於矩陣或數據框,所有交叉驗證運行必須具有相同的長度。

由於str(bc_rf_predict_prob) > [1] matrix [1:200, 1:2],這意味着str(bc_test$Class)應該有一個匹配的維度。

這聽起來像你只想要bc_rf_predict_prob的第一列矢量,但我不能確定沒有看數據。

+0

謝謝。需要將[,2]添加到bc_rf_predict_prob的末尾。因此bc_rf_predict_prob [,2]。 – mpg 2014-09-23 19:46:57

+0

不客氣。樂於幫助。 – 2014-09-23 19:54:52