入门 NLP 自然语言处理,如何使用 Python 绘制词云图?

2023-06-0318:16:19人工智能与大数据Comments1,282 views字数 1465阅读模式

如何使用 Python 制作词云图。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

作为 NLP 中最简单的部分,非常适合大家作为入门 NLP 自然语言处理的案例。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

本篇文章会给大家详细介绍绘制词云图的整体流程和具体实现代码,如果你需要完整的代码,可以加粉丝群进行下载。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

先上效果图:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

入门 NLP 自然语言处理,如何使用 Python 绘制词云图?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

在制作词云图之前说一下需求,我们有非常多的新闻文章,我们想要从这些新闻文章中分析出现频率最多的关键字是什么,然后按照关键字出现的频率绘制出词云图。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

先来说说整体的步骤:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

      • 导入依赖包
      • 配置matplotlib
      • 导入新闻数据
      • 删除空数据
      • 分词
      • 去停用词
      • 统计词频
      • 做词云

导入依赖包的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

import jieba  # 分词包
import numpy  # numpy计算包
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
from wordcloud import WordCloud  # 词云包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

matplotlib的配置代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

matplotlib.rcParams['figure.figsize'] = (10.0, 5.0)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

读取新闻数据的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

df = pd.read_csv("../data/entertainment_news.csv", encoding='utf-8')文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

删除空数据的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

df = df.dropna()
content = df.content.values.tolist()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

分词的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

segment = []
for line in content:
try:
segs = jieba.lcut(line)  # 分词
for seg in segs:
if len(seg) > 1 and seg != '\r\n':
segment.append(seg)
except:
print(line)
continue文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

去除停用词的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

words_df = pd.DataFrame({'segment': segment})
stopwords = pd.read_csv("../data/stopwords.txt", index_col=False, quoting=3, sep="\t", names=['stopword'],
encoding='utf-8')  # quoting=3全不引用
words_df = words_df[~words_df.segment.isin(stopwords.stopword)]文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

统计词频的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

words_stat = words_df.groupby('segment').agg({"segment": numpy.size}).rename(columns={'segment': '计数'})
words_stat = words_stat.reset_index().sort_values(by=["计数"], ascending=False)
print(words_stat.head())文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

做词云的代码如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

wordcloud = WordCloud(font_path="../data/simhei.ttf", background_color="white", max_font_size=80)
word_frequence = {x[0]: x[1] for x in words_stat.head(1000).values}
wordcloud = wordcloud.fit_words(word_frequence)
plt.imshow(wordcloud)
plt.show()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

最终效果图如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

入门 NLP 自然语言处理,如何使用 Python 绘制词云图?文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ai/44229.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ai/44229.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定