train() 中的 ROC 度量,插入符包

标签 r machine-learning neural-network r-caret roc

df分为训练数据帧和测试数据帧。训练数据帧分为训练数据帧和测试数据帧。因变量Y是二进制(因子),值为 0 和 1。我试图用此代码(神经网络,插入符号包)预测概率:

library(caret)

model_nn <- train(
  Y ~ ., training,
  method = "nnet",
  metric="ROC",
  trControl = trainControl(
    method = "cv", number = 10,
    verboseIter = TRUE,
    classProbs=TRUE
  )
)

model_nn_v2 <- model_nn
nnprediction <- predict(model_nn, testing, type="prob")
cmnn <-confusionMatrix(nnprediction,testing$Y)
print(cmnn) # The confusion matrix is to assess/compare the model

但是,它给了我这个错误:

    Error: At least one of the class levels is not a valid R variable name; 
This will cause errors when class probabilities are generated because the
 variables names will be converted to  X0, X1 . Please use factor levels 
that can be used as valid R variable names  (see ?make.names for help).

我不明白“可用作有效 R 变量名称的使用因子级别”是什么意思。因变量Y已经是一个因子,但不是有效的 R 变量名称?

PS:如果你删除classProbs=TRUE,代码就可以正常工作。在trainControl()metric="ROC"train() 。然而,"ROC"指标是我对我的案例中最佳模型进行比较的指标,因此我尝试使用“ROC”指标创建一个模型。

编辑:代码示例:

# You have to run all of this BEFORE running the model
classes <- c("a","b","b","c","c")
floats <- c(1.5,2.3,6.4,2.3,12)
dummy <- c(1,0,1,1,0)
chr <- c("1","2","2,","3","4")
Y <- c("1","0","1","1","0")
df <- cbind(classes, floats, dummy, chr, Y)
df <- as.data.frame(df)
df$floats <- as.numeric(df$floats)
df$dummy <- as.numeric(df$dummy)

classes <- c("a","a","a","b","c")
floats <- c(5.5,2.6,7.3,54,2.1)
dummy <- c(0,0,0,1,1)
chr <- c("3","3","3,","2","1")
Y <- c("1","1","1","0","0")
df <- cbind(classes, floats, dummy, chr, Y)
df <- as.data.frame(df)
df$floats <- as.numeric(df$floats)
df$dummy <- as.numeric(df$dummy)

最佳答案

这里有两个单独的问题。

第一条是错误消息,它说明了一切:您必须使用 “0”、“1” 之外的其他内容作为因因子变量的 Y

在构建数据帧df之后,您至少可以通过两种方式来做到这一点;第一个提示错误消息,即使用 make.names:

df$Y <- make.names(df$Y)
df$Y
# "X1" "X1" "X1" "X0" "X0"

第二种方法是使用levels函数,通过该函数您可以显式控制名称本身;在这里再次显示它,名称为 X0X1

levels(df$Y) <- c("X0", "X1")
df$Y
# [1] X1 X1 X1 X0 X0
# Levels: X0 X1

添加上述任一行后,显示的 train() 代码将顺利运行(将 training 替换为 df),但是它仍然不会产生任何 ROC 值,而是给出警告:

Warning messages:
1: In train.default(x, y, weights = w, ...) :
  The metric "ROC" was not in the result set. Accuracy will be used instead.

这给我们带来了第二个问题:为了使用 ROC 指标,您必须在 train 的 trControl 参数中添加 summaryFunction = TwoClassSummary ():

model_nn <- train(
  Y ~ ., df,
  method = "nnet",
  metric="ROC",
  trControl = trainControl(
    method = "cv", number = 10,
    verboseIter = TRUE,
    classProbs=TRUE,
    summaryFunction = twoClassSummary # ADDED
  )
)

使用您提供的玩具数据运行上述代码片段仍然会出现错误(缺少 ROC 值),但这可能是由于此处使用的数据集非常小,加上大量 CV 折叠,因此不会发生这种情况使用您自己的完整数据集(如果我将 CV 折叠减少到 number=3,它就可以正常工作)...

关于train() 中的 ROC 度量,插入符包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50634918/

相关文章:

python - 为什么softmax分类器梯度除以批量大小(CS231n)?

machine-learning - SVM - 什么是功能边际?

python, neurolab, 一步步训练

c++ - 在 C++ 中使用 Rcpp header

r - 调用其中的函数时如何获取R脚本文件名?

R 使用值列表作为色标

neural-network - 池化层或卷积层后的激活函数?

r - 带有多个 header 的httr请求

python - 在 Windows 上安装 Darknet 时出错

python-3.x - TensorFlow:dataset.train.next_batch 是如何定义的?