简体   繁体   English

错误:`data` 和 `reference` 应该是具有相同级别的因子'不返回混淆矩阵

[英]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 的阈值为估计概率创建一个混淆矩阵,但我不断收到错误消息“错误: datareference应该是具有相同水平的因素”。 Whats wrong?怎么了? See code below见下面的代码

I have tried to write the code我试过写代码

TURN PROBS INTO CLASSES AND DISPLAY FREQUENCIES将问题转化为类和显示频率

p_class = ifelse (probs_truth$estimated > 0.5, 1, 0)
table(p_class)

CALCULATING CONFUSION MATRIX计算混淆矩阵

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.

相关问题 混淆矩阵错误:错误:`data`和`reference`应该是具有相同水平的因子 - Confusion Matrix Error: Error: `data` and `reference` should be factors with the same levels Adaboost:混淆矩阵的问题 - `data` 和 `reference` 应该是具有相同水平的因素 - Adaboost: Problem with confusion matrix - `data` and `reference` should be factors with the same levels 错误:`data` 和 `reference` 应该是相同级别的因子。 Logistic 回归的混淆矩阵 - Error: `data` and `reference` should be factors with the same levels. Confusion matrix for Logistic Regression R:RF模型中的混淆矩阵返回错误:数据和“参考”应该是具有相同水平的因子 - R: Confusion matrix in RF model returns error: data` and `reference` should be factors with the same levels 混淆矩阵错误:数据和参考因素必须具有相同的水平数 - Error in Confusion Matrix : the data and reference factors must have the same number of levels 混淆矩阵中的“具有相同水平的因素” - 'factors with the same levels' in Confusion Matrix 什么地方出了错? 错误:`data` 和 `reference` 应该是具有相同水平的因素 - What went wrong? Error: `data` and `reference` should be factors with the same levels ConfusionMatrix 错误:`data` 和 `reference` 应该是具有相同水平的因素 - ConfusionMatrix Error: `data` and `reference` should be factors with the same levels r - 错误:`data` 和 `reference` 应该是具有相同水平的因素 - r - Error: `data` and `reference` should be factors with the same levels 使用混淆矩阵`data`和`reference`的错误应该是具有相同水平的因素 - error using confusionMatrix `data` and `reference` should be factors with the same levels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM