【问题标题】:How to change size of confusion matrix in fastai?如何改变fastai中混淆矩阵的大小?
【发布时间】:2021-08-20 02:24:57
【问题描述】:

我正在使用以下代码在fastai 中绘制混淆矩阵:

interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()

但我最终得到了一个超小的矩阵,因为我有大约 20 个类别:

我找到了 sklearns 的相关问题,但不知道如何将其应用于 fastai(因为我们不直接使用 pyplot

【问题讨论】:

    标签: python machine-learning data-visualization confusion-matrix fast-ai


    【解决方案1】:

    如果你检查函数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))
    

    【讨论】:

      猜你喜欢
      • 2018-11-27
      • 2021-07-21
      • 1970-01-01
      • 2021-01-04
      • 2017-02-18
      • 2021-10-26
      • 2017-03-07
      • 2014-07-09
      • 1970-01-01
      相关资源
      最近更新 更多