我ranger
用来适应随机森林。作为评估指标,我使用的是 roc-auc-score,按cvAUC
. 做出预测后,当我尝试评估 auc 分数时,我得到一个错误:Format of predictions is invalid. It couldn't be coerced to a list
. 我认为这是由于预测包含Level
显示预测的独特级别的一部分。但是,我无法摆脱那部分。下面是最小的可重现示例,它会引发错误:
library(caret)
install.packages("cvAUC")
library(cvAUC)
# Columns for training set
cat.column <- c("cat", "dog", "monkey", "shark", "seal")
num.column <- c(1,2,5,7,9)
class <- c(0,1,0,0,1)
train.set <- data.frame(num.column, cat.column, class)
# Columns for test set
cat.column <- c("cat", "elephant-shrew", "monkey", "monkey", "seal")
num.column <- c(1,11,5,6,8)
class <- c(1,0,1,0,1)
test.set <- data.frame(num.column, cat.column, class)
# Drop the target variable from the test set
target.test <- test.set["class"]
test.set <- test.set[,!names(test.set) %in% "class"]
# Fit random forest
rf = ranger(formula = as.factor(class) ~ . , data = train.set, verbose = FALSE)
# Get predictions
pred <- predict(rf, test.set)
predictions <- pred$predictions
# Get AUC score
auc <- AUC(as.factor(predictions), as.factor(unlist(target.test)), label.ordering = NULL)
cat(auc)