簡體   English   中英

錯誤:`data` 和 `reference` 應該是具有相同級別的因子'不返回混淆矩陣

[英]Error: `data` and `reference` should be factors with the same levels' doesn't return confusion matrix

我有一個包含估計概率和實際結果的 csv 文件。 我想使用 0.5 的閾值為估計概率創建一個混淆矩陣,但我不斷收到錯誤消息“錯誤: datareference應該是具有相同水平的因素”。 怎么了? 見下面的代碼

我試過寫代碼

將問題轉化為類和顯示頻率

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)

我希望返回一個混淆矩陣表

后續代碼對我有用,希望它有所幫助:我制作了一個小數據集,我猜它與您的數據相似。

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

根據您的 ifelse 語句 ('estimated2') 向您的數據集添加一列估計值。

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

確保 '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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM