【发布时间】:2016-09-30 04:15:50
【问题描述】:
我有一个不平衡的数据,我想进行分层交叉验证并使用精确召回 auc 作为我的评估指标。
我在带有分层索引的r包插入符中使用prSummary,在计算性能时遇到错误。
以下是可以复制的样本。我发现计算 p-r auc 的样本只有 10 个,并且由于不平衡,只有一个类,因此无法计算 p-r auc。 (我发现只有十个样本来计算 p-r auc 是因为我修改了 prSummary 来强制这个函数打印出数据)
library(randomForest)
library(mlbench)
library(caret)
# Load Dataset
data(Sonar)
dataset <- Sonar
x <- dataset[,1:60]
y <- dataset[,61]
# make this data very imbalance
y[4:length(y)] <- "M"
y <- as.factor(y)
dataset$Class <- y
# create index and indexOut
seed <- 1
set.seed(seed)
folds <- 2
idxAll <- 1:nrow(x)
cvIndex <- createFolds(factor(y), folds, returnTrain = T)
cvIndexOut <- lapply(1:length(cvIndex), function(i){
idxAll[-cvIndex[[i]]]
})
names(cvIndexOut) <- names(cvIndex)
# set the index, indexOut and prSummaryCorrect
control <- trainControl(index = cvIndex, indexOut = cvIndexOut,
method="cv", summaryFunction = prSummary, classProbs = T)
metric <- "AUC"
set.seed(seed)
mtry <- sqrt(ncol(x))
tunegrid <- expand.grid(.mtry=mtry)
rf_default <- train(Class~., data=dataset, method="rf", metric=metric, tuneGrid=tunegrid, trControl=control)
这里是错误信息:
Error in ROCR::prediction(y_pred, y_true) :
Number of classes is not equal to 2.
ROCR currently supports only evaluation of binary classification tasks.
【问题讨论】: