Using Python to draw a obfuscation matrix (Confusion_matrix)

Source: Internet
Author: User
Summary

As to the classification problem, we often need to analyze the experiment result through the visual confusion matrix to get the parameter idea, this article describes how to use Python to draw the confusing matrix (Confusion_matrix), this article only provides the code, gives the necessary annotation. Code

#-*-coding:utf-8-*-from sklearn.metrics import confusion_matrix import matplotlib.pyplot as PLT import NumPy as NP #lab Els shows you different categories of code, such as the demo here there are 13 categories labels = [' A ', ' B ', ' C ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', #y_true代表真实的lab The El value y_pred represents the predicted lavel value y_true = Np.loadtxt ('. /data/re_label.txt ') y_pred = Np.loadtxt ('.. /data/pr_label.txt ') Tick_marks = Np.array (range (len (labels)) + 0.5 def plot_confusion_matrix (cm, title= ' confusion Mat
    Rix ', cmap=plt.cm.binary): plt.imshow (cm, interpolation= ' nearest ', Cmap=cmap) plt.title (title) Plt.colorbar () Xlocations = Np.array (range (len (labels)) plt.xticks (xlocations, labels, rotation=90) plt.yticks (Xlocations, L Abels) Plt.ylabel (' True label ') Plt.xlabel (' predicted label ') cm = Confusion_matrix (y_true, y_pred) np.set_print Options (precision=2) cm_normalized = Cm.astype (' float ')/cm.sum (Axis=1) [:, Np.newaxis] Print cm_normalized plt.figure ( Figsize= (8), dpi=120) Ind_array = Np.arange (len (labels)) x, y = Np.meshgrid (Ind_array, Ind_array) for X_val, y_val in Zip (X.flatten (), Y.flatten ()): c = Cm_normalized[y_v Al][x_val] If C > 0.01:plt.text (x_val, Y_val, "%0.2f"% (c,), color= ' Red ', fontsize=7, va= ' center ', ha= ' CE
Nter ') # offset The Tick PLT.GCA (). Set_xticks (Tick_marks, Minor=true) PLT.GCA (). Set_yticks (Tick_marks, Minor=true) PLT.GCA (). Xaxis.set_ticks_position (' None ') PLT.GCA (). Yaxis.set_ticks_position (' None ') Plt.grid (True, which= ' Minor ', linestyle= '-') PLT.GCF (). Subplots_adjust (bottom=0.15) Plot_confusion_matrix (cm_normalized, title= ' Normalized Confusion matrix ') # Show Confusion Matrix Plt.savefig (' ...
 /data/confusion_matrix.png ', format= ' png ') plt.show ()
Result

Instructions

Follow the comments in your code to replace labels, y_true, y_pred with your own data. Reference

How to use Python to draw the confusion matrix.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.