confusion_matrix#
- sklearn.metrics.confusion_matrix(y_true, y_pred, *, labels=None, sample_weight=None, normalize=None)[源代码]#
计算混淆矩阵以评估分类的准确性。
根据定义,混乱矩阵 \(C\) 使得 \(C_{i, j}\) 等于组中已知的观察数 \(i\) 并预计在团体中 \(j\) .
因此,在二元分类中,真阴性的计数是 \(C_{0,0}\) ,假阴性是 \(C_{1,0}\) ,真正的积极因素是 \(C_{1,1}\) 假阳性是 \(C_{0,1}\) .
阅读更多的 User Guide .
- 参数:
- y_true形状类似阵列(n_samples,)
地面真相(正确)目标值。
- y_pred形状类似阵列(n_samples,)
分类器返回的估计目标。
- labels类数组的形状(n_classes),默认值=None
用于索引矩阵的标签列表。这可以用于重新排序或选择标签的子集。如果
None
给出的是那些至少出现一次的y_true
或y_pred
按排序顺序使用。- sample_weight形状类似数组(n_samples,),默认=无
样本重量。
Added in version 0.18.
- normalize' true ',' pred ','},默认=无
在真实(行)、预测(列)条件或所有总体上规范化混淆矩阵。如果为“无”,混淆矩阵将不被规范化。
- 返回:
- C形状的ndarray(n_classes,n_classes)
混淆矩阵,其第i行和第j列条目指示真实标签为第i类且预测标签为第j类的样本数量。
参见
ConfusionMatrixDisplay.from_estimator
在给定估计量、数据和标签的情况下绘制混淆矩阵。
ConfusionMatrixDisplay.from_predictions
给定真实和预测标签,绘制混淆矩阵。
ConfusionMatrixDisplay
混乱矩阵可视化。
引用
[1]Wikipedia entry for the Confusion matrix (维基百科和其他参考文献可能对轴使用不同的约定)。
示例
>>> 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]])
在二进制情况下,我们可以提取真阳性等,如下所示:
>>> tn, fp, fn, tp = confusion_matrix([0, 1, 0, 1], [1, 1, 1, 0]).ravel() >>> (tn, fp, fn, tp) (np.int64(0), np.int64(2), np.int64(1), np.int64(1))