【问题标题】:Plotting pairs of bins in a histogram for comparison with seaborn在直方图中绘制成对的 bin 以与 seaborn 进行比较
【发布时间】:2021-08-23 07:51:18
【问题描述】:

我有一个 Dataframe,其中包含一些 ML 模型,其中 2 列分别用于训练和测试精度

evaluations_df
Out[13]: 
                    Model  train_accuracy  test_accuracy
0     Logistic Regression      100.000000      86.956522
1           Decision Tree       99.065421      84.782609
2           Random Forest       92.523364      82.608696
3            Ada Boosting      100.000000      89.130435
4       Gradient Boosting      100.000000      84.782609
5       Nearest Neighbors       88.785047      82.608696
6  Support Vector Machine       93.457944      82.608696
7             Naive Bayes       99.065421      89.130435

我想绘制类似这样的图:

x 值是模型的数量,其刻度将被模型名称替换,y 值将是每个准确度指标的一对。

我尝试了类似的方法:

sns.histplot(data=evaluations_df, x=range(len(evaluations_df)), y=['train_accuracy', 'test_accuracy'],
                 color=['r', 'b'],
                 shrink=0.8,
                 multiple='dodge')

但它会引发以下错误:

ValueError: Length of list vectors must match length of `data` when both are used, but `data` has length 8 and the vector passed to `y` has length 2.

我似乎无法将 y 值解压缩为带有该列表的一对箱子。

【问题讨论】:

  • 直接调用sns.histplot,需要将dataframe转成long form

标签: python python-3.x seaborn


【解决方案1】:

不使用 seaborn,只使用 pandas,您可以这样做:

evaluations_df.plot.bar(x='Model', y=['train_accuracy', 'test_accuracy'])

【讨论】:

  • 谢谢,它按照我的预期完成了我想要的,但是仍然可以用 seaborn 做吗?它的展示总是看起来更好。
  • 您可以在开始时致电sns.set() 获得seaborn 风格。
  • @JohanC 正是我所需要的!
猜你喜欢
  • 2020-10-21
  • 2021-08-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
  • 2013-04-27
  • 1970-01-01
  • 2021-10-29
  • 2022-01-25
相关资源
最近更新 更多