【问题标题】:How to save Confusion Matrix plot so that I can call it for future reference?如何保存混淆矩阵图以便我可以调用它以供将来参考?
【发布时间】:2020-08-24 17:20:09
【问题描述】:

我正在使用这个最新的函数 sklearn.metrics.plot_confusion_matrix 来绘制我的混淆矩阵。

cm = plot_confusion_matrix(classifier,X , y_true,cmap=plt.cm.Greens)

当我执行该单元格时,混淆矩阵图按预期显示。我的问题是我想稍后将该图用于另一个单元格。当我在另一个单元格中调用 cm 时,它只显示该对象的位置。

>>> cm
>>> <sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at 0x1af790ac6a0>

调用 plt.show() 也不起作用

【问题讨论】:

    标签: python-3.x matplotlib scikit-learn jupyter


    【解决方案1】:

    为了让您的问题按预期工作,您应该这样做cm.plot()

    证明

    让我们尝试以可重现的方式进行:

    from sklearn.metrics import plot_confusion_matrix
    from sklearn.datasets import make_classification
    from sklearn.ensemble import RandomForestClassifier
    
    np.random.seed(42)
    X, y = make_classification(1000, 10, n_classes=2)
    clf = RandomForestClassifier()
    clf.fit(X,y)
    cm = plot_confusion_matrix(clf, X , y, cmap=plt.cm.Greens)
    

    您可以稍后将 cm 对象绘制为:

    cm.plot(cmap=plt.cm.Greens);
    

    供您参考。您可以通过以下方式访问cm 对象可用的方法:

    [method for method in dir(cm) if not method.startswith("__")]
    ['ax_',
     'confusion_matrix',
     'display_labels',
     'figure_',
     'im_',
     'plot',
     'text_']
    

    【讨论】:

    • 我怎样才能直接将它保存到磁盘呢?
    猜你喜欢
    • 2017-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2020-02-12
    相关资源
    最近更新 更多