【问题标题】:ROC curve error in randomForestrandomForest中的ROC曲线错误
【发布时间】:2014-11-18 01:45:08
【问题描述】:

我正在尝试从下面创建一条 ROC 曲线。我收到一个错误,指出预测中的错误(bc_rf_predict_prob,bc_test$Class): 预测和标签的交叉验证运行次数必须相等。

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$Class): 预测和标签的交叉验证运行次数必须相等。

我认为这是因为我这样做的事实:

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

我得到一个矩阵作为结果,其中包含两列良性及其概率列表以及第二列恶性及其概率列表。我的逻辑告诉我我应该只有一个概率向量。

【问题讨论】:

  • 您是否尝试过使用str(predict(bc_rf, type="prob", bc_test)) 来确定@​​987654324@ 的数据类型/结构?
  • 同意这可能是问题所在。我得到矩阵 [1:200, 1:2] 1 0.12 1 1 0 1 1 1 0.05 1 ... - attr(, "dimnames")=List of 2 ..$ : chr [1:200] "3" "4" "5" "12" ... ..$ : chr [1:2] "良性" "恶性" - attr(, "class")= chr [1:2] “矩阵”“投票”
  • 那么-我该怎么办?

标签: r random-forest roc


【解决方案1】:

根据page 9 of the ROCR Library documentation,prediction 函数有两个必需的输入,predictions 和labels,它们必须具有相同的维度。

对于矩阵或数据框,所有交叉验证运行必须具有相同的长度。

由于str(bc_rf_predict_prob) &gt; [1] matrix [1:200, 1:2],这意味着str(bc_test$Class) 应该有一个匹配的维度。

听起来你只想要bc_rf_predict_prob 的第一列向量,但不看数据我无法确定。

【讨论】:

  • 谢谢。需要将 [,2] 添加到 bc_rf_predict_prob 的末尾。因此 bc_rf_predict_prob[,2].
猜你喜欢
  • 2018-04-25
  • 2017-07-23
  • 2021-07-30
  • 2019-02-27
  • 2018-01-03
  • 2019-12-26
  • 2014-09-27
  • 1970-01-01
  • 2019-12-03
相关资源
最近更新 更多