【问题标题】:ConfusionMatrix Error: `data` and `reference` should be factors with the same levelsConfusionMatrix 错误:`data` 和 `reference` 应该是具有相同级别的因素
【发布时间】:2021-12-20 23:53:21
【问题描述】:

大家好,我面临这样的错误,我是新手,我不知道这是什么意思。我使用神经网络函数对 Titanic 数据集进行分类

有什么建议吗??

str(Prediksi)

 num [1:143] 0.393 0.393 0.393 0.393 0.393 ...

str(测试)

num [1:143, 1:10] 1 1 1 1 1 1 1 1 1 1 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:143] "1" "3" "9" "15" ...
  ..$ : chr [1:10] "(Intercept)" "Survived" "Pclass" "Sexmale" ...

confusionMatrix(Prediksi,test)

Error: `data` and `reference` should be factors with the same levels.

【问题讨论】:

  • 您的测试集的边缘集可能与您的训练集不同。

标签: r


【解决方案1】:

confusionMatrix 函数中,两个术语都需要是具有相同水平的因子。你的Prediksitest 都不是一个因素。

例如,

confusionMatrix(iris$Species, iris$Species)

这将在str(iris$Species) 返回时起作用

Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

但是,caret::confusionMatrix 不允许character

看看confusionMatrix(iris$Species, as.character(iris$Species))

这将返回

Error: `data` and `reference` should be factors with the same levels.

另外,numeric 变量将不起作用。

confusionMatrix(iris$Species, iris$Sepal.Length)

Error: `data` and `reference` should be factors with the same levels.

您没有提供足够的关于您的任务的信息,因此很难说如何解决您的问题,但请尝试将 confusionMatrix 中的两个术语作为具有相同水平的因素。

【讨论】:

    猜你喜欢
    • 2020-03-16
    • 2019-06-20
    • 2019-01-04
    • 2021-03-18
    • 2019-11-21
    • 2021-09-20
    • 2020-08-16
    • 2020-09-17
    • 2020-08-17
    相关资源
    最近更新 更多