简体   繁体   中英

how can I save a scikit-learn confusion matrix as png

How can I save a confusion matrix as png? I've saw this answer:

How to save Confusion Matrix plot so that I can call it for future reference?

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')

The result that I'm getting is just a black png image.

I think, you should update sklearn to the latest version and then use:

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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