我在fastai
中使用以下代码绘制混淆矩阵:
interp = ClassificationInterpretation.from_learner(learn)interp.plot_confusion_matrix()
但是由于我有大约20个类别,最终得到的矩阵非常小:
我找到了与sklearn相关的提问,但不知道如何将其应用到fastai(因为我们没有直接使用pyplot
)。
回答:
如果你查看函数ClassificationInterpretation.plot_confusion_matrix
的代码(在文件fastai / interpret.py中),你会看到以下内容:
def plot_confusion_matrix(self, normalize=False, title='Confusion matrix', cmap="Blues", norm_dec=2, plot_txt=True, **kwargs): "Plot the confusion matrix, with `title` and using `cmap`." # This function is mainly copied from the sklearn docs cm = self.confusion_matrix() if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] fig = plt.figure(**kwargs) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) tick_marks = np.arange(len(self.vocab)) plt.xticks(tick_marks, self.vocab, rotation=90) plt.yticks(tick_marks, self.vocab, rotation=0)
关键在于这一行fig = plt.figure(**kwargs)
,因此,plot_confusion_matrix
函数会将其参数传递给绘图函数。
所以你可以使用以下任一选项:
dpi=xxx
(例如dpi=200
)figsize=(xxx, yyy)
关于它们之间的关系,请参见StackOverflow上的这篇文章:https://stackoverflow.com/a/47639545/1603480
因此,在你的情况下,你可以这样做:
interp.plot_confusion_matrix(figsize=(12, 12))
混淆矩阵将看起来像这样:
顺便提一下:这也适用于其他绘图函数,例如
interp.plot_top_losses(20, nrows=5, figsize=(25, 25))