如何将 scikit-learn 混淆矩阵保存为 png

Ton*_*dez 5 python scikit-learn

如何将混淆矩阵保存为 png 格式?我看过这个答案:

如何保存混淆矩阵图以便我可以调用它以供将来参考?

from sklearn.metrics import plot_confusion_matrix

y_true = [0,1,1,1,0]
y_pred = [1,1,1,1,0]

IC = type('IdentityClassifier', (), {"predict": lambda i : i, "_estimator_type": "classifier"})

cm = plot_confusion_matrix(IC, y_pred, y_true, normalize='true',  values_format='.2%')
    
cm.figure_.savefig('confusion_matrix.png')
Run Code Online (Sandbox Code Playgroud)

我得到的结果只是一个黑色的 png 图像。

小智 2

我认为,你应该将 sklearn 更新到最新版本,然后使用:

from sklearn.metrics import ConfusionMatrixDisplay


y_true = [0,1,1,1,0]
y_pred = [1,1,1,1,0]

IC = type('IdentityClassifier', (), {"predict": lambda i : i, "_estimator_type": "classifier"})

cm=ConfusionMatrixDisplay.from_estimator(IC, y_pred, y_true, normalize='true',  values_format='.2%')

cm.figure_.savefig('confusion_matrix.png')
Run Code Online (Sandbox Code Playgroud)