在Python中生成单词云

Word Cloud是一种数据可视化技术,用于表示文本数据,其中每个单词的大小表示其频率或重要性。重要的文本数据点可以使用单词云突出显示。词云被广泛用于分析社交网络网站的数据。

null

要在Python中生成word cloud,需要的模块有:matplotlib、pandas和wordcloud。要安装这些软件包,请运行以下命令:

pip install matplotlibpip install pandaspip install wordcloud

用于生成单词云的数据集来自UCI机器学习库。它包括YouTube上对流行艺术家视频的评论。 数据集链接: https://archive.ics.uci.edu/ml/machine-learning-databases/00380/

以下是实施情况:

Python3

# Python program to generate WordCloud
# importing all necessary modules
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import pandas as pd
# Reads 'Youtube04-Eminem.csv' file
df = pd.read_csv(r "Youtube04-Eminem.csv" , encoding = "latin-1" )
comment_words = ''
stopwords = set (STOPWORDS)
# iterate through the csv file
for val in df.CONTENT:
# typecaste each val to string
val = str (val)
# split the value
tokens = val.split()
# Converts each token into lowercase
for i in range ( len (tokens)):
tokens[i] = tokens[i].lower()
comment_words + = " " .join(tokens) + " "
wordcloud = WordCloud(width = 800 , height = 800 ,
background_color = 'white' ,
stopwords = stopwords,
min_font_size = 10 ).generate(comment_words)
# plot the WordCloud image
plt.figure(figsize = ( 8 , 8 ), facecolor = None )
plt.imshow(wordcloud)
plt.axis( "off" )
plt.tight_layout(pad = 0 )
plt.show()


输出:

图片[1]-在Python中生成单词云-yiteyi-C++库

上面的单词cloud是使用Youtube04 Eminem生成的。数据集中的csv文件。一个有趣的任务可能是使用数据集中的其他csv文件生成单词云。

词云的优势:

  1. 分析客户和员工的反馈。
  2. 确定新的搜索引擎优化关键字的目标。

词云的缺点:

  1. 词云并非适用于所有情况。
  2. 数据应该根据上下文进行优化。

参考: https://en.wikipedia.org/wiki/Tag_cloud

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享