【发布时间】: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