混合矩阵可视化函数封装

混合矩阵可视化函数封装

在这里插入图片描述
混合矩阵和交叉表类似,只不过表现的形式不一样, 都是在评判模型是否满足业务场景的一种检测方法, 可以直观的查看查全率.
函数定义如下:

import itertools
def plot_confusion_matrix(cm, classes,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=0)
    plt.yticks(tick_marks, classes)

    threshold = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="center",
                 color="white" if cm[i, j] > threshold else "black")#若对应格子上面的数量不超过阈值则,上面的字体为白色,为了方便查看

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

参数详解:

cm : 为生成的交叉表的数据,一般不用自己算,可以通过confusion_matrix方法来获得:
如:

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_)   # y_test 为训练样本已知的目标值, y_ 为训练样本预测值,返回一个ndarray数据

classes: 目标值的分类,传入一个列表,有几类列表就有几个元素
title: 表名 一般写上recall (查全率也叫回调率)
cmap : 选择自己喜欢的表的颜色

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值