上一篇文章(连接)咱们对COVID19_line_list数据集进行了清洗以及初步分析。本文中咱们将分析如何用词云来展现文本信息的概要。python
好比咱们从词云百度百科截取文字,制做词云。简单来讲,词云就是重要单词的可视化,以下图。 git
咱们将用词云回答这两个问题。github
python 做为一个万能胶水语言,各类有用的轮子天然不胜枚举。wordcloud 即是专门用于制做词云的包。 安装方式很简单,pip便可。bash
数据咱们采用上篇中清理好的数据,这里我将清理好的数据保存为新的csv文件(COVID19_line_list_data_cleaned.csv)。app
第一步,导入必要的库。oop
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from PIL import Image
from wordcloud import ImageColorGenerator
from wordcloud import WordCloud
import jieba
复制代码
# read the data
line_list_data_cleaned_file = 'data/COVID19_line_list_data_cleaned.csv'
line_list_data_raw_df = pd.read_csv(line_list_data_cleaned_file)
复制代码
咱们须要分析的是symptom 和summary 两列的信息。 wordcloud 分析的文本为str 格式,所以咱们须要将dataframe 每一行的数据组合成一个str 格式。post
# prepare the text by using str.cat
all_symptoms = line_list_data_raw_df['symptom'].str.cat(sep=',')
print(type(all_symptoms))
复制代码
<class 'str'>
复制代码
能够看到all_symptoms 已是str 格式。咱们先分析symptom 列,后续会处理summary列的信息。字体
借用经典的案例代码,咱们先用默认的参数制做词云。ui
# fast show wordcloud
wordcloud = WordCloud().generate(all_symptoms)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
复制代码
固然能解决,wordCould 类带有一些初始化参数,好比min_font_size控制最小的词字体大小,像素大小经过width和height 来调节。默认的collocations 为True,用于合并重要性/频次至关的文本。设定这些参数,咱们能够垂手可得的改善的词云画面。spa
# change the resolution and cancel the collocations
wordcloud = WordCloud(
min_font_size=10,
width=800,
height=400,
collocations=False).generate(all_symptoms)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
复制代码
一款经典版的词云就出炉了,看上去很不错。咱们的第一个问题也有了答案: fever 和cough 是最多见的症状。
这里有一幅人类体形图,咱们也能够将这些症状的词条做为tag 刻画在人物肖像上。这里须要用到wordcloud的mask 参数。mask 顾名思义就是用于掩盖一些像素。
# modern wordcloud
mask = np.array(Image.open('data/human_body_shape.png'))
print(mask)
复制代码
[[255 255 255 0]
[255 255 255 0]
[255 255 255 0]
...
[255 255 255 0]
[255 255 255 0]
[255 255 255 0]]
[[255 255 255 0]
[255 255 255 0]
[255 255 255 0]
...
[255 255 255 0]
[255 255 255 0]
[255 255 255 0]]
[[255 255 255 0]
[255 255 255 0]
[255 255 255 0]
...
[255 255 255 0]
[255 255 255 0]
[255 255 255 0]]]
复制代码
再次建立wordcloud,代码几乎和上次雷同,仅仅是添加一个mask参数,以及设定图像的线条宽度contour_width 以及颜色contour_color。
wordcloud = WordCloud(
background_color="white",
min_font_size=10,
width=800,
height=400,
mask=mask,
collocations=False,
contour_width=2,
contour_color='black').generate(all_symptoms)
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
复制代码
很明显,summary 列的信息量要远远大于symptom,下面咱们能够分析该列数据。 此次咱们选择一幅彩色图像,我把human换成robot。几乎一样的代码,再次运行。
mask = np.array(Image.open('data/robot.png'))
all_summary = line_list_data_raw_df['summary'].str.cat(sep=',')
image_colors = ImageColorGenerator(mask)
wordcloud = WordCloud(
background_color="white",
min_font_size=10,
width=800,
height=400,
mask=mask,
collocations=False,
contour_width=1,
contour_color='black').generate(all_summary)
plt.figure()
plt.imshow(
wordcloud.recolor(
color_func=image_colors),
interpolation="bilinear")
plt.axis("off")
plt.show()
复制代码
结果。。。oops, 说好的机器人呢?怎么只有两个眼睛和几个大门牙,必定是mask出了问题。
咱们打印一下建立的mask矩阵。一堆堆零,边框明明是白色的,为何是零呢?datacamp 博客给出了必定的解释。总之,零不是咱们想要的。
[[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
...
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
...
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
...
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
...
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
...
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
...
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
...
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]]
复制代码
解决方案也很简单,替换0为255,而后从新制做词云。
mask[mask == 0] = 255
复制代码
可爱的机器人终于出现了。
回到咱们开始提到的问题,咱们能够看到summary主要是关于新确认的(new confirmed)一些COVID 案例,病人(patient)可能和Wuhan相关。并且咱们能够看到样本中male 彷佛比female 多一些。
到此咱们的两个问题都圆满的经过词云回答了。
回到开篇的词云图,咱们展现了一份中文词云。若是直接借用咱们今天的代码可能会出现一些问题。这里咱们仅仅贴出中文词云制做的代码,以及一点注意事项。
ciyun.csv 就是从百度词条随便截取的,你能够换成任意的文章。
ciyun = 'data/ciyun.csv'
with open(ciyun) as f:
ciyun_str = f.read()
def jieba_processing_txt(text):
mywordlist = []
seg_list = jieba.cut(text, cut_all=False)
liststr = "/ ".join(seg_list)
for myword in liststr.split('/'):
if len(myword.strip()) > 1:
mywordlist.append(myword)
return ' '.join(mywordlist)
font = 'data/SourceHanSerifCN-Light.otf' # 能够下载或者用电脑的自带的字体
wordcloud = WordCloud(
min_font_size=10,
width=800,
height=400,
collocations=False,font_path=font).generate(jieba_processing_txt(ciyun_str))
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
复制代码
本文介绍了经典版以及画面嵌套版的词云制做。使用词云能够一目了然的获取海量文本内容的关键信息。词云制做过程当中的一些坑咱们也进行了掩埋: