代码之家  ›  专栏  ›  技术社区  ›  Chris

ROC公制in train(),插入符号包

  •  0
  • Chris  · 技术社区  · 6 年前

    这个 df 在列车和测试数据帧中拆分。列车数据帧在训练和测试数据帧中被分割。因变量 Y 是值为0和1的二进制(因子)。我试图用这段代码(神经网络,caret软件包)预测概率:

    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变量名?。

    附言:如果你删除代码,代码会很好地工作 classProbs=TRUE 在里面 trainControl() metric="ROC" 在里面 train() .然而 "ROC" metric是我的最佳模型的比较指标,因此我尝试使用“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)
    
    1 回复  |  直到 6 年前
        1
  •  7
  •   desertnaut SKZI    6 年前

    这里有两个独立的问题。

    第一个是错误消息,它说明了一切:您必须使用 "0", "1" 价值观 对于因变量 Y

    在构建了数据框架之后,至少可以通过两种方法来实现这一点 df ; 第一个是提示错误消息,即使用 make.names :

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

    第二种方法是使用 levels 函数,通过该函数可以显式控制名称本身;在此处再次显示名称 X0 X1

    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指标,您必须添加 summaryFunction = twoClassSummary trControl 的参数 列车() :

    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 )。。。