从wordcloud导出到Python列表

2024-09-21 00:20:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我在CSV中有一个列,列有特定项目的描述(苏格兰威士忌)

在成功生成了一个wordcloud,其中包含50个更常见的词来描述威士忌的味道之后,我想将这50个词导出到一个列表中

我使用的代码如下:

SMS=train_df[train_df.category=="Single Malt Scotch"]['description'].values


from wordcloud import WordCloud,STOPWORDS

stopwords=set(STOPWORDS)
stopwords=STOPWORDS.add('whisky')
stopwords=STOPWORDS.add('distillery')
stopwords=STOPWORDS.add('whiskies')
stopwords=STOPWORDS.add('bottle')
stopwords=STOPWORDS.add('bottling')
stopwords=STOPWORDS.add('bottles')
stopwords=STOPWORDS.add('year')
stopwords=STOPWORDS.add('flavor')
stopwords=STOPWORDS.add('finish')
stopwords=STOPWORDS.add('one')
stopwords=STOPWORDS.add('old')
stopwords=STOPWORDS.add('note')
stopwords=STOPWORDS.add('year')
stopwords=STOPWORDS.add('palate')
stopwords=STOPWORDS.add('notes')
stopwords=STOPWORDS.add('age')
stopwords=STOPWORDS.add('s')
stopwords=STOPWORDS.add('u')

def show_wordcloud(data,title=None):
    wc=WordCloud(background_color="black", max_words=50,stopwords=STOPWORDS, max_font_size= 40)
    wc.generate(" ".join(data))
    fig=fig = plt.figure(figsize=[9,6], dpi=80)
    plt.axis('off')
    if title:
        fig.suptitle(title,fontsize=16)
        fig.subplots_adjust(top=1)
        plt.imshow(wc.recolor( colormap= 'Pastel2' , random_state=17), alpha=1,interpolation='bilinear')
        plt.show()

show_wordcloud(SMS,title="Wordcloud for Single Malt Whisky")

生成的wordcloud如下所示:

wordcloud picture

我想从python列表中的图像中获取这50个单词


Tags: adddf列表titleshowfigtrainplt

热门问题