MCPcopy Create free account
hub / github.com/OpenGVLab/UniFormerV2 / plot_confusion_matrix

Function plot_confusion_matrix

slowfast/visualization/utils.py:48–89  ·  view source on GitHub ↗

A function to create a colored and labeled confusion matrix matplotlib figure given true labels and preds. Args: cmtx (ndarray): confusion matrix. num_classes (int): total number of classes. class_names (Optional[list of strs]): a list of class names. fig

(cmtx, num_classes, class_names=None, figsize=None)

Source from the content-addressed store, hash-verified

46
47
48def plot_confusion_matrix(cmtx, num_classes, class_names=None, figsize=None):
49 """
50 A function to create a colored and labeled confusion matrix matplotlib figure
51 given true labels and preds.
52 Args:
53 cmtx (ndarray): confusion matrix.
54 num_classes (int): total number of classes.
55 class_names (Optional[list of strs]): a list of class names.
56 figsize (Optional[float, float]): the figure size of the confusion matrix.
57 If None, default to [6.4, 4.8].
58
59 Returns:
60 img (figure): matplotlib figure.
61 """
62 if class_names is None or type(class_names) != list:
63 class_names = [str(i) for i in range(num_classes)]
64
65 figure = plt.figure(figsize=figsize)
66 plt.imshow(cmtx, interpolation="nearest", cmap=plt.cm.Blues)
67 plt.title("Confusion matrix")
68 plt.colorbar()
69 tick_marks = np.arange(len(class_names))
70 plt.xticks(tick_marks, class_names, rotation=45)
71 plt.yticks(tick_marks, class_names)
72
73 # Use white text if squares are dark; otherwise black.
74 threshold = cmtx.max() / 2.0
75 for i, j in itertools.product(range(cmtx.shape[0]), range(cmtx.shape[1])):
76 color = "white" if cmtx[i, j] > threshold else "black"
77 plt.text(
78 j,
79 i,
80 format(cmtx[i, j], ".2f") if cmtx[i, j] != 0 else ".",
81 horizontalalignment="center",
82 color=color,
83 )
84
85 plt.tight_layout()
86 plt.ylabel("True label")
87 plt.xlabel("Predicted label")
88
89 return figure
90
91
92def plot_topk_histogram(tag, array, k=10, class_names=None, figsize=None):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected