如何在fastai中更改混淆矩阵的大小?

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

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

但是由于我有大约20个类别,最终得到的矩阵非常小:

small confusion matrix

我找到了与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))

混淆矩阵将看起来像这样:

big confusion matrix

顺便提一下:这也适用于其他绘图函数,例如

interp.plot_top_losses(20, nrows=5, figsize=(25, 25))

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注