我正在处理关于多标签分类的实际数据和来自分类器的预测数据。实际数据包括三个类别(c1、c2和c3),同样地,预测数据也包括三个类别(c1、c2和c3)。数据如下:
Actual_data Predicted_data
c1 c2 c3 c1 c2 c3
1 1 0 1 1 1
1 1 0 1 0 1
1 0 1 0 1 1
0 1 1 1 0 0
1 0 0 1 1 0
1 1 1 1 0 1
在多标签分类中,一个文档可能属于多个类别。上述数据中,1表示文档属于特定类别,0表示文档不属于特定类别。
实际数据的第一行表示文档属于c1和c2类,不属于c3类。同样地,预测数据的第一行表示文档属于c1、c2和c3类。
最初,我使用了R编程来找到实际数据和预测数据之间的混淆矩阵。我将这些数据框保存在y_actual和y_predict中。
y_actual<-as.matrix(Actual_data)
y_predict<-as.matrix(Predicted_data)
xtab<-table(y_actual,y_predict)
输出的 xtab 是:
y_predict
y_actual 0 1
0 1 5
1 5 7
接下来,我使用 R 语言的 caret 包创建了混淆矩阵,如下所示:
library(caret)
confusionMatrix(xtab)
Confusion Matrix and Statistics
y_predict
y_actual 0 1
0 1 5
1 5 7
Accuracy : 0.4444
95% CI : (0.2153, 0.6924)
No Information Rate : 0.6667
P-Value [Acc > NIR] : 0.9856
Kappa : -0.25
Mcnemar's Test P-Value : 1.0000
Sensitivity : 0.16667
Specificity : 0.58333
Pos Pred Value : 0.16667
Neg Pred Value : 0.58333
Prevalence : 0.33333
Detection Rate : 0.05556
Detection Prevalence : 0.33333
Balanced Accuracy : 0.37500
'Positive' Class : 0
在这种情况下,我没有获得多标签混淆矩阵,而是获得了二进制标签混淆矩阵。我希望在y-actual和y-predict中都有c1、c2、c3的混淆矩阵,而不是0、1。
然后我在互联网上搜索,发现utiml包用于R中的多标签分类,但它没有提供所需的输出。然后我尝试使用python的scikit包进行多标签分类,以下是代码。
import numpy as np
from sklearn.metrics import multilabel_confusion_matrix
y_actual = np.array([[1, 1, 0],
[1, 1, 0],
[1, 0, 1],
[0, 1, 1],
[1, 0, 0],
[1, 1, 1]])
y_predict = np.array([[1, 1, 1],
[1, 0, 1],
[0, 1, 1],
[1, 0, 0],
[1, 1, 0],
[1, 0, 1]])
matrix = multilabel_confusion_matrix(y_actual, y_predict)
print(matrix)
print(classification_report(y_actual,y_predict))
程序的输出是:
[[[0 1]
[1 4]]
[[0 2]
[3 1]]
[[1 2]
[1 2]]]
precision recall f1-score support
0 0.80 0.80 0.80 5
1 0.33 0.25 0.29 4
2 0.50 0.67 0.57 3
micro avg 0.58 0.58 0.58 12
macro avg 0.54 0.57 0.55 12
weighted avg 0.57 0.58 0.57 12
samples avg 0.53 0.61 0.54 12
在这种情况下,我无法按标签输出结果。 有人可以帮我确定在任何平台(R编程、Python或Weka)中需要使用哪种软件包来获取上述实际和预测数据的多标签混淆矩阵吗? 输出的混淆矩阵需要是y_actual和y_predict的c1、c2和c3的3*3矩阵。
y_predict
y_actual c1 c2 c3
c1 4
c2 1
c3 2
这里的对角元素表明实际上它属于 c1,而分类器预测它属于 c1。c2和c3同理。我的问题是如何获得混淆矩阵的其他值,因为这是多标签分类问题。这个问题不是多类别分类问题,而是多标签分类问题。