简体   繁体   English

绘制具有多个标签的混淆矩阵 sklearn

[英]Plot confusion matrix sklearn with multiple labels

I am plotting a confusion matrix for a multiple labelled data, where labels look like:我正在为多标签数据绘制混淆矩阵,其中标签如下所示:

label1: 1, 0, 0, 0标签 1: 1, 0, 0, 0

label2: 0, 1, 0, 0标签 2: 0, 1, 0, 0

label3: 0, 0, 1, 0标签 3: 0, 0, 1, 0

label4: 0, 0, 0, 1标签 4: 0, 0, 0, 1

I am able to classify successfully using the below code.我能够使用以下代码成功分类。 I only need some help to plot confusion matrix.我只需要一些帮助来绘制混淆矩阵。

    for i in range(4):
        y_train= y[:,i]
        print('Train subject %d, class %s' % (subject, cols[i]))
        lr.fit(X_train[::sample,:],y_train[::sample])
        pred[:,i] = lr.predict_proba(X_test)[:,1]

I used the following code to print confusion matrix, but it always return a 2X2 matrix我使用以下代码打印混淆矩阵,但它总是返回一个 2X2 矩阵

prediction = lr.predict(X_train)

print(confusion_matrix(y_train, prediction))

I found a function that can plot the confusion matrix which generated from sklearn .我找到了一个可以绘制从sklearn生成的混淆矩阵的sklearn

import numpy as np


def plot_confusion_matrix(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

    Usage
    -----
    plot_confusion_matrix(cm           = cm,                  # confusion matrix created by
                                                              # sklearn.metrics.confusion_matrix
                          normalize    = True,                # show proportions
                          target_names = y_labels_vals,       # list of names of the classes
                          title        = best_estimator_name) # title of graph

    Citiation
    ---------
    http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html

    """
    import matplotlib.pyplot as plt
    import numpy as np
    import itertools

    accuracy = np.trace(cm) / float(np.sum(cm))
    misclass = 1 - accuracy

    if cmap is None:
        cmap = plt.get_cmap('Blues')

    plt.figure(figsize=(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=45)
        plt.yticks(tick_marks, target_names)

    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",
                     color="white" if cm[i, j] > thresh else "black")
        else:
            plt.text(j, i, "{:,}".format(cm[i, j]),
                     horizontalalignment="center",
                     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()

It will look like this它看起来像这样在此处输入图片说明

This works the best for me :这对我来说最有效:

from sklearn.metrics import multilabel_confusion_matrix
y_unique = y_test.unique()
mcm = multilabel_confusion_matrix(y_test, y_pred, labels = y_unique)
mcm

I see this is still an open issue in sklearn 's repository:我看到这仍然是sklearn存储库中的一个悬而未决的问题:

https://github.com/scikit-learn/scikit-learn/issues/3452 https://github.com/scikit-learn/scikit-learn/issues/3452

However there have been some attempts at implementing it.然而,已经有一些实施它的尝试。 From the same #3452 thread issue:来自相同的 #3452 线程问题:

https://github.com/Magellanea/scikit-learn/commit/514287c1d5dad2f0ab4918dc4da5cf7053fe6734#diff-b04acd877dd793f28ae7be13a999ed88R187 https://github.com/Magellanea/scikit-learn/commit/514287c1d5dad2f0ab4918dc4da5cf7053fe6734#diff-b04acd877dd793f28ae7be13a999ed88R187

You can check the code proposed in the function and see if that fits your needs.您可以检查函数中建议的代码,看看它是否符合您的需要。

from sklearn.metrics import multilabel_confusion_matrix

mul_c = multilabel_confusion_matrix(
    test_Y,
    pred_k,
    labels=["benign", "dos","probe","r2l","u2r"])
mul_c

I found an easy solution with sklearn and seaborn libraries.我用 sklearn 和 seaborn 库找到了一个简单的解决方案。

from sklearn.metrics import confusion_matrix, classification_report
from matplotlib import pyplot as plt
import seaborn as sns

def plot_confusion_matrix(y_test,y_scores, classNames):
    y_test=np.argmax(y_test, axis=1)
    y_scores=np.argmax(y_scores, axis=1)
    classes = len(classNames)
    cm = confusion_matrix(y_test, y_scores)
    print("**** Confusion Matrix ****")
    print(cm)
    print("**** Classification Report ****")
    print(classification_report(y_test, y_scores, target_names=classNames))
    con = np.zeros((classes,classes))
    for x in range(classes):
        for y in range(classes):
            con[x,y] = cm[x,y]/np.sum(cm[x,:])

    plt.figure(figsize=(40,40))
    sns.set(font_scale=3.0) # for label size
    df = sns.heatmap(con, annot=True,fmt='.2', cmap='Blues',xticklabels= classNames , yticklabels= classNames)
    df.figure.savefig("image2.png")

classNames = ['A', 'B', 'C', 'D', 'E'] 
plot_confusion_matrix(y_test,y_scores, classNames) 
#y_test is your ground truth
#y_scores is your predicted probabilities

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM