[英]Confusion matrix when all classes are not predicted with caret package in R
我有一個分類模型predict(model, test.x)
來評估具有11個類的數據的模型,預測的結果是:
table(predicted_class)
0 1 2 3 5 6 8 10
7 6 232 11 74 58 1 1
我的測試標簽(真相)是:
table(test.y)
0 1 2 3 4 5 6 7 8 9 10
105 16 78 25 14 74 12 9 23 15 19
當我想使用插入符號包獲取混淆矩陣時,出現此錯誤消息,因為模型未預測類7和9:
caret::confusionMatrix(test.y, predicted_class, mode = "everything")
Error in confusionMatrix.default(test.y, predicted_class, :
the data cannot have more levels than the reference
當預測中缺少某些因子水平時,如何獲得混淆矩陣:如何為缺少的類(在本例中為4、7和9)的預測類自動添加0
通過結合union
因素使水平相同
all_class <- union(predicted_class, test.y)
newtable <- table(factor(predicted_class, all_class), factor(test.y, all_class))
caret::confusionMatrix(newtable)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.