【问题标题】:Plotted Confusion Matrix values overlapping each other, total classes 90绘制的混淆矩阵值相互重叠,总类 90
【发布时间】:2018-07-29 01:06:27
【问题描述】:

如何增加 x 轴和 y 轴标签之间的间距,使混淆矩阵内的绘制结果不重叠?

【问题讨论】:

    标签: python scikit-learn keras metrics confusion-matrix


    【解决方案1】:

    我找到了这段代码,经过一些小的修改后,我发现它不能正常工作。

    def plot_confusion_matrix_2(cm,
                          target_names,
                          title='Confusion matrix',
                          cmap=None,
                          normalize=True):
    """
    given a sklearn confusion matrix (cm), make a nice plot
    
    Arguments
    ---------
    cm:           confusion matrix from sklearn.metrics.confusion_matrix
    
    target_names: given classification classes such as [0, 1, 2]
                  the class names, for example: ['high', 'medium', 'low']
    
    title:        the text to display at the top of the matrix
    
    cmap:         the gradient of the values displayed from matplotlib.pyplot.cm
                  see http://matplotlib.org/examples/color/colormaps_reference.html
                  plt.get_cmap('jet') or plt.cm.Blues
    
    normalize:    If False, plot the raw numbers
                  If True, plot the proportions
    
    
    Citiation
    ---------
    http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
    
    """
    FONT_SIZE = 8
    
    accuracy = np.trace(cm) / float(np.sum(cm))
    misclass = 1 - accuracy
    
    if cmap is None:
        cmap = plt.get_cmap('Blues')
    
    plt.figure(figsize=(8*2, 6*2))    # 8, 6
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    
    if target_names is not None:
        tick_marks = np.arange(len(target_names))
        plt.xticks(tick_marks, target_names, rotation=90, fontsize=FONT_SIZE)
        plt.yticks(tick_marks, target_names, fontsize=FONT_SIZE)
    
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    
    
    thresh = cm.max() / 1.5 if normalize else cm.max() / 2
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        if normalize:
            plt.text(j, i, "{:0.4f}".format(cm[i, j]),
                     horizontalalignment="center",
                     fontsize=FONT_SIZE,
                     color="white" if cm[i, j] > thresh else "black")
        else:
            plt.text(j, i, "{:,}".format(cm[i, j]),
                     horizontalalignment="center",
                     fontsize=FONT_SIZE,
                     color="white" if cm[i, j] > thresh else "black")
    
    
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label\naccuracy={:0.4f}; misclass={:0.4f}'.format(accuracy, misclass))
    plt.show()
    

    这就是我的称呼

    plot_confusion_matrix_2(cm, cm_classes, normalize=False, title='Confusion Matrix')
    

    使用figsizeFONT_SIZE 参数,直到您对结果满意为止。

    【讨论】:

      猜你喜欢
      • 2016-01-31
      • 1970-01-01
      • 2016-06-04
      • 2020-09-01
      • 2020-10-24
      • 2017-10-17
      • 2018-12-19
      • 1970-01-01
      相关资源
      最近更新 更多