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

错误:`data`和'reference`应该是具有相同级别的因子。使用confusionmatrix(插入符号)

  •  2
  • antecessor  · 技术社区  · 7 年前

    使用时出错 confusionMatrix() 函数来自 caret 包裹。为了重现这个例子,我使用 Sonar 数据集来自 mlbench 包裹。

    library(mlbench)
    data(Sonar)
    
    rows <- sample(nrow(Sonar))
    Sonar <- Sonar[rows, ]
    
    
    split <- round(nrow(Sonar) * 0.6)
    adiestramiento <- Sonar[1:split, ]
    experimental <- Sonar[(split + 1):nrow(Sonar), ]
    
    model <- glm(Class ~ ., family = binomial(link = "logit"), adiestramiento)
    p <- predict(model, experimental, type = "response")
    p_class <- ifelse(p > 0.5, "M", "R")
    
    library(caret)
    confusionMatrix(p_class, experimental[["Class"]])
    

    我在跑步时遇到的错误 混乱矩阵()

    错误: data reference 应该是具有相同水平的因素`

    我都查过了 p_class experimental[["Class"]] 拥有相同数量的对象(83)。

    知道怎么回事吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Julius Vainora    7 年前

    问题是 data 或者,在这种情况下, p_class 必须是一个因素。所以,我们应该使用

    confusionMatrix(factor(p_class), experimental[["Class"]])
    # Confusion Matrix and Statistics
    # 
    #           Reference
    # Prediction  M  R
    #          M 17 20
    #          R 33 13
    # ...
    
    推荐文章