词云分析

import pandas as pd
df_news=pd.read_table(r'C:\Users\CDAer\Desktop\data\car.txt',
                   names=['category','theme','url','content'])
df_news.head(3)
df_news.shape
df_news['category'].value_counts()
财经    500
教育    500
健康    500
军事    500
娱乐    500
科技    500
时尚    500
汽车    500
体育    500
文化    500
Name: category, dtype: int64
#500条娱乐新闻的词云分析
df_relax=df_news[df_news.category=='娱乐']
list_relax=df_relax.content.values.tolist()

jieba分词

import jieba
stopwords=pd.read_csv(r'C:\Users\CDAer\Desktop\data\stopwords.txt',sep='\t',
                     quoting=3,names=['stopword'])
stopwords_list=stopwords['stopword'].values.tolist()
words=[]
for line in list_relax:
    seg=jieba.lcut(line)
    for word in seg:
        if word =='\n' or len(word)<=1:
            continue
        elif word in stopwords_list:
            continue
        else:
            words.append(word)         

计算词频

df_cloud=pd.DataFrame({'cloud_words':words})
df_cloud.head()
cloud_words
0网友
1出图
2显示
3陈宝国
4妻子
count=df_cloud.groupby(by=['cloud_words'])['cloud_words'].count()
import numpy as np
#统计词频
words_count=df_cloud.groupby(by=['cloud_words'])['cloud_words'].agg({'count':np.size})
D:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: FutureWarning: using a dict on a Series for aggregation
is deprecated and will be removed in a future version
words_count_sort=words_count.reset_index().sort_values(by=['count'],ascending=False)
words_count_sort.head()
cloud_wordscount
13659电影443
16725观众342
6980导演294
16074节目259
990中国257

绘制词云

from wordcloud import WordCloud
import matplotlib.pyplot as plt
%matplotlib inline
WordCloud?
#配置基本的词云参数
cloud=WordCloud(
        font_path='C:/Windows/Fonts/simhei.ttf')
#用处理好的字符+词频数据,生成词云
word_freq={x[0]:x[1] for x in words_count_sort.values}
pic_cloud=cloud.fit_words(word_freq)
#将词云绘制出
plt.imshow(pic_cloud)
plt.axis('off')
plt.show()

词云参数的调整

plt.imshow?
# 'none', 'nearest', 'bilinear', 'bicubic',
#     'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser',
#     'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc',
#     'lanczos'
##配置画布的大小
plt.rcParams['figure.figsize']=(10,5)
#配置基本的词云参数
cloud=WordCloud(
        font_path='C:/Windows/Fonts/simhei.ttf',
        width=1000,
        height=500,
        background_color='white')
#用处理好的字符+词频数据,生成词云
word_freq={x[0]:x[1] for x in words_count_sort.values}
pic_cloud=cloud.fit_words(word_freq)
#将词云绘制出
plt.imshow(pic_cloud,interpolation='bilinear')
plt.axis('off')
plt.show()

[外链图片转存失败(img-GqkoZOjI-1562757157121)(output_22_0.png)]

任意形状的词云

from PIL import Image
pic=np.array(Image.open('C:\\Users\\CDAer\\Desktop\\data\\people.jpg'))
WordCloud?
##配置画布的大小
plt.rcParams['figure.figsize']=(10,5)
#配置基本的词云参数
cloud=WordCloud(
        font_path='C:/Windows/Fonts/simhei.ttf',
        width=1000,
        height=500,
        background_color='white',
        mask=pic)
#用处理好的字符+词频数据,生成词云
word_freq={x[0]:x[1] for x in words_count_sort.values}
pic_cloud=cloud.fit_words(word_freq)
#将词云绘制出
plt.imshow(pic_cloud,interpolation='bilinear')
plt.axis('off')
plt.show()

[外链图片转存失败(img-lCGJhQyX-1562757157122)(output_26_0.png)]


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值