[英]Error: `data` and `reference` should be factors with the same levels' doesn't return confusion matrix
I have a csv file containing estimated probabilities and actual results.我有一个包含估计概率和实际结果的 csv 文件。 I want to create a confusion matrix using a threshold of 0.5 for the estimated probabilities but i keep getting the error message 'Error: data
and reference
should be factors with the same levels.'我想使用 0.5 的阈值为估计概率创建一个混淆矩阵,但我不断收到错误消息“错误: data
和reference
应该是具有相同水平的因素”。 Whats wrong?怎么了? See code below见下面的代码
I have tried to write the code我试过写代码
p_class = ifelse (probs_truth$estimated > 0.5, 1, 0)
table(p_class)
predicted = p_class
actual = probs_truth$truth
library(caret)
result = confusionMatrix (data=predicted, reference=actual)
print(result)
I expected a confusion matrix table to be returned我希望返回一个混淆矩阵表
Subsequent code workes for me, hope it helps: I made a small dataset which I guess resembles your data.后续代码对我有用,希望它有所帮助:我制作了一个小数据集,我猜它与您的数据相似。
library(data.table)
probs_truth <- data.table(estimated = c(0.5, 0.3, 0.7, 0.8, 0.1), actual = c(1, 0, 0, 1, 0))
Added a column to your dataset with the estimated values according to your ifelse statement ('estimated2').根据您的 ifelse 语句 ('estimated2') 向您的数据集添加一列估计值。
probs_truth$estimated2 = ifelse (probs_truth$estimated > 0.5, 1, 0)
Made sure 'estimated2' and 'actual' are factors.确保 'estimated2' 和 'actual' 是因素。
probs_truth$estimated2 <- as.factor(probs_truth$estimated2)
probs_truth$actual <- as.factor(probs_truth$actual)
head(probs_truth)
library(caret)
result = confusionMatrix (data=probs_truth$estimated2, reference=probs_truth$actual)
print(result)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.