@@ -29,12 +29,31 @@ def plot_confusion_matrix(
2929 title : str = "Confusion matrix" ,
3030 cmap = None ,
3131 fname = None ,
32+ show_scores : bool = True ,
3233 noshow : bool = False ,
3334 backend : str = "Agg" ,
3435 format_string : Optional [str ] = None ,
3536):
36- """Render the confusion matrix and return matplotlib's figure with it.
37+ """
38+ Render the confusion matrix and return matplotlib's figure with it.
3739 Normalization can be applied by setting `normalize=True`.
40+
41+ Args:
42+ cm: Numpy array of (N,N) shape - confusion matrix array
43+ class_names: List of [N] names of the classes
44+ figsize:
45+ fontsize:
46+ normalize: Whether to apply normalization for each row of CM
47+ title: Title of the confusion matrix
48+ cmap:
49+ fname: Filename of the rendered confusion matrix
50+ show_scores: Show scores in each cell
51+ noshow:
52+ backend:
53+ format_string:
54+
55+ Returns:
56+ Matplotlib's figure
3857 """
3958 import matplotlib
4059
@@ -64,26 +83,12 @@ def plot_confusion_matrix(
6483 if format_string is None :
6584 format_string = ".3f" if normalize else "d"
6685
67- thresh = (cm .max () + cm .min ()) / 2.0
68- for i , j in itertools .product (range (cm .shape [0 ]), range (cm .shape [1 ])):
69- if np .isfinite (cm [i , j ]):
70- plt .text (
71- j ,
72- i ,
73- format (cm [i , j ], format_string ),
74- horizontalalignment = "center" ,
75- fontsize = fontsize ,
76- color = "white" if cm [i , j ] > thresh else "black" ,
77- )
78- else :
79- plt .text (
80- j ,
81- i ,
82- "N/A" ,
83- horizontalalignment = "center" ,
84- fontsize = fontsize ,
85- color = "black" ,
86- )
86+ if show_scores :
87+ thresh = (cm .max () + cm .min ()) / 2.0
88+ for i , j in itertools .product (range (cm .shape [0 ]), range (cm .shape [1 ])):
89+ text = format (cm [i , j ], format_string ) if np .isfinite (cm [i , j ]) else "N/A"
90+ color = "white" if cm [i , j ] > thresh else "black"
91+ plt .text (j , i , text , horizontalalignment = "center" , fontsize = fontsize , color = color )
8792
8893 plt .ylabel ("True label" )
8994
0 commit comments