【问题标题】:Not appropriate position of numbers in confusion matrix Python混淆矩阵Python中数字的位置不合适
【发布时间】:2020-05-07 23:32:29
【问题描述】:

我有这样的代码:

plt.figure(figsize=(8,5))
confusion_matrix = pd.crosstab(y_test, predictions, rownames=["Observed"], colnames=["Anticipated"])
sns.heatmap(confusion_matrix, annot=True, fmt= "d")
plt.show()

尽管如此,每个方格内的数字并不在每个方格的中心,如下所示。如何更改此代码以使数字位于每个正方形的中心位置?

【问题讨论】:

  • 很遗憾,我无法复制您的问题。我在 Windows 10 Python 3.7 上获得了以您的代码为中心的数字。你能分享一下你正在使用的 python、pandas、matplotlib 和 seaborn 的版本吗?
  • 我有 Python 3.8、Pandas 0.25.1、Mtplotlib 3.1.1 和 Seaborn 0.9.0

标签: python pandas seaborn


【解决方案1】:

this post on datascience.stackexchange.com 所示,matplotlib 3.1.1 破坏了sns.heatmap()。那里的答案建议降级到matplotlib 3.1.0。但是我已经在我的机器上安装了 3.1.2 并且它正在工作。所以你现在也许可以升级。

y_test = np.array(
    [
        "foo", "foo", "foo", "foo",
        "bar", "bar", "bar", "bar",
        "foo", "foo", "foo"
    ], dtype=object
)
predictions = np.array(
    [
        "one", "one", "one", "two",
        "one", "one", "one", "two",
        "two", "two", "one"
    ], dtype=object
)

plt.figure(figsize=(8, 5))
confusion_matrix = pd.crosstab(
    y_test, predictions, rownames=["Observed"], colnames=["Anticipated"]
)
sns.heatmap(confusion_matrix, annot=True, fmt="d")
plt.show()

这是pip list 的结果以及相关包。

Package            Version   Location
------------------ --------- -------------------------------------
matplotlib         3.1.2
pandas             0.25.1
seaborn            0.9.0

【讨论】:

  • 尽管如此,当我尝试在 Anaconda Prompt 中升级 matplotlin 时遇到问题,因为我有错误:由于 EnvironmentError 无法安装软件包:[WinError 5] Permission denied: path to my anaconda3
  • 我在尝试升级 matplotlib 时遇到了同样的问题。我建议关闭尽可能多的东西,然后再试一次。我意识到我还有一个打开的图,这阻止了 dll 锁的释放。
猜你喜欢
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-09
  • 1970-01-01
  • 2016-07-25
  • 2015-03-09
  • 2018-01-25
  • 1970-01-01
相关资源
最近更新 更多