【问题标题】:Making bar plot of different clusters制作不同聚类的条形图
【发布时间】:2021-05-31 23:12:17
【问题描述】:

我目前正在学习 K-means,所以现在我正在用 Python 编写一个程序来确定彼此相似的不同文本集群。

所以现在我得到了两个不同集群的结果(使用了一些虚构的词,但其他一切都是一样的)。

print(dfs)  = [           features     score
0  America 0.577350
1            new 0.288675
2        president 0.288675
3          Biden 0.288675
,       features     score
0       Corona 0.593578
1  COVID-19 0.296789
2     research 0.296789
3    health 0.158114]

而dfs是以下类型

type(dfs) = list

还有以下内容:

type(dfs[0]) = pandas.core.frame.DataFrame

但是如何轻松地为 dfs 中的每个集群创建条形图,您可以在其中看到每个单词附加的分数?

提前致谢!

【问题讨论】:

    标签: python matplotlib bar-chart k-means unsupervised-learning


    【解决方案1】:

    遍历dfs 列表以访问单个数据框,然后使用df.plot.barx='featuresy='score' 作为参数来绘制相对于同一数据框的条形图。使用 plot 函数的结果轴将每个条形的分数附加到 features 列中。为此,使用矩形锚点的x 和条形的height 作为annotate 函数的参数,从条形图轴迭代每个补丁。

    ...
    ...
    
    fig, axes = plt.subplots(1, len(dfs))
    for num, df in enumerate(dfs):
        ax = df.plot.bar(x='features', y='score', ax=axes[num])
        for p in ax.patches:
            ax.annotate(f'{p.get_height():.4f}', xy=(p.get_x() * 1.01, p.get_height() * 1.01))
        axes[num].tick_params(axis='x', labelrotation=30)
        axes[num].set_title(f'Dataframe #{num}')
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多