最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 如何实现python绘制混淆矩阵?

    正文概述    2020-06-13   344

    大家从python基础到如今的入门,想必都对python有一定基础,今天小编给大家带来一个关于python的高阶内容——绘制混淆矩阵,一起来看下吧~

    介绍:

    混淆矩阵通过表示正确/不正确标签的计数来表示模型在表格格式中的准确性。

    计算/绘制混淆矩阵:

    以下是计算混淆矩阵的过程。

    您需要一个包含预期结果值的测试数据集或验证数据集。

    • 对测试数据集中的每一行进行预测。

    • 从预期的结果和预测计数:

    • 每个类的正确预测数量。

    • 每个类的错误预测数量,由预测的类组织。

    然后将这些数字组织成表格或矩阵,如下所示:

    • Expected down the side:矩阵的每一行都对应一个预测的类。

    • Predicted across the top:矩阵的每一列对应于一个实际的类。

    然后将正确和不正确分类的计数填入表格中。

    Reading混淆矩阵:

    一个类的正确预测的总数进入该类值的预期行,以及该类值的预测列。

    以同样的方式,一个类别的不正确预测总数进入该类别值的预期行,以及该类别值的预测列。

    对角元素表示预测标签等于真实标签的点的数量,而非对角线元素是分类器错误标记的元素。混淆矩阵的对角线值越高越好,表明许多正确的预测。

    Python绘制混淆矩阵 :

    import itertools
     
    import numpy as np
     
    import matplotlib.pyplot as plt
     
    from sklearn import svm, datasets
     
    from sklearn.model_selection import train_test_split
     
    from sklearn.metrics import confusion_matrix
     
    # import some data to play with
     
    iris = datasets.load_iris()
     
    X = iris.data
     
    y = iris.target
     
    class_names = iris.target_names
     
    # Split the data into a training set and a test set
     
    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
     
    # Run classifier, using a model that is too regularized (C too low) to see
     
    # the impact on the results
     
    classifier = svm.SVC(kernel='linear', C=0.01)
     
    y_pred = classifier.fit(X_train, y_train).predict(X_test)
     
    def plot_confusion_matrix(cm, classes,
     
    normalize=False,
     
    title='Confusion matrix',
     
    cmap=plt.cm.Blues):
     
    """
     
    This function prints and plots the confusion matrix.
     
    Normalization can be applied by setting `normalize=True`.
     
    """
     
    if normalize:
     
    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
     
    print("Normalized confusion matrix")
     
    else:
     
    print('Confusion matrix, without normalization')
     
    print(cm)
     
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
     
    plt.title(title)
     
    plt.colorbar()
     
    tick_marks = np.arange(len(classes))
     
    plt.xticks(tick_marks, classes, rotation=45)
     
    plt.yticks(tick_marks, classes)
     
    fmt = '.2f' if normalize else 'd'
     
    thresh = cm.max() / 2.
     
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
     
    plt.text(j, i, format(cm[i, j], fmt),
     
    horizontalalignment="center",
     
    color="white" if cm[i, j] > thresh else "black")
     
    color="white" if cm[i, j] > thresh else "black")
     
    plt.tight_layout()
     
    plt.ylabel('True label')
     
    plt.xlabel('Predicted label')
     
    # Compute confusion matrix
     
    cnf_matrix = confusion_matrix(y_test, y_pred)
     
    np.set_printoptions(precision=2)
     
    # Plot non-normalized confusion matrix
     
    plt.figure()
     
    plot_confusion_matrix(cnf_matrix, classes=class_names,
     
    title='Confusion matrix, without normalization')
     
    # Plot normalized confusion matrix
     
    plt.figure()
     
    plot_confusion_matrix(cnf_matrix, classes=class_names, normalize=True,
     
    title='Normalized confusion matrix')
     
    plt.show()

    如何实现python绘制混淆矩阵?

    Confusion matrix, without normalization
     
    [[13 0 0]
     
    [ 0 10 6]
     
    [ 0 0 9]]
     
    Normalized confusion matrix
     
    [[ 1. 0. 0. ]
     
    [ 0. 0.62 0.38]
     
    [ 0. 0. 1. ]]

    好了,大家可以消化学习下哦~如需了解更多python实用知识,点击进入起源地模板网教学中心


    起源地下载网 » 如何实现python绘制混淆矩阵?

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元