目的:评估分类器准确性
函数:sklearn.metrics.confusion_matrix(y_true, y_pred, labels=None, sample_weight=None)
输入:
- y_true:实际的目标结果
- y_pred:预测的结果
- labels: 标签,对结果中的string进行排序, 顺序对应0、1、2
- sample_weight:样本的权重?
输出:
- 一个矩阵,shape=[y中的类型数,y中的类型数]
- 矩阵中每个值表征分类的准确性
- 第0行第0列的数表示y_true中值为0,y_pred中值也为0的个数
- 第0行第1列的数表示y_true中值为0,y_pred中值为1的个数
示例:
>>> from sklearn.metrics import confusion_matrix
>>> y_true = [2, 0, 2, 2, 0, 1]
>>> y_pred = [0, 0, 2, 2, 0, 2]
>>> confusion_matrix(y_true, y_pred)
array([[2, 0, 0], [0, 0, 1], [1, 0, 2]])
>>> 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]])
网友评论