获取等于类标签数量的多类混淆矩阵

时间:2018-08-14 04:32:44

标签: scikit-learn classification confusion-matrix

我在sklearn中训练了随机森林分类器,以预测多分类问题。

我的数据集有四个类标签。但是我的代码创建了2x2混淆矩阵

y_predict = rf.predict(X_test)
conf_mat = sklearn.metrics.confusion_matrix(y_test, y_predict)
print(conf_mat)

输出:

[[0,   0]

 [394, 39]]

如何获取4x4混淆矩阵来分析TP,TN,FP,FN。

1 个答案:

答案 0 :(得分:0)

来自
的文档 http://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html

y_true = ["cat", "ant", "cat", "cat", "ant", "bird"]
y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"]
confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])

结果:

array([[2, 0, 0], 
       [0, 0, 1], 
       [1, 0, 2]])