python英文词云代码_python做词云 (WordCloud)

本文介绍了如何使用Python的WordCloud库创建词云,包括安装步骤、核心函数详解、实例演示,重点讲解了fit_words函数的正确使用和停用词的处理。通过实例,读者将学会利用WordCloud制作个性化教育水平词云。
摘要由CSDN通过智能技术生成

python做词云 (WordCloud)

1. 安装

某个教程给出的方法,到[这里][1]下载相应的wordcolud,然后到相应目录pip安装。

其实直接

pip install wordcloud

就ok了 ,进入python。 import wordcloud成功即可。

20180526112040342548.png

##2. 文档简要说明

20180526112040447033.png

可以看到文档主要就3个主要的函数,目前主要介绍WordCloud模块以及相关的函数。

WordCloud()

class wordcloud.WordCloud(font_path=None, width=400, height=200, margin=2, ranks_only=None, prefer_horizontal=0.9, mask=None, scale=1, color_func=None, max_words=200, min_font_size=4, stopwords=None, random_state=None, background_color=‘black‘, max_font_size=None, font_step=1, mode=‘RGB‘, relative_scaling=0.5, regexp=None, collocations=True, colormap=None, normalize_plurals=True)

font_path:字体位置,中文的时候需要制定一些。

prefer_horizontal:float,水平方向的拟合次数,如果小于1,一旦水平方向不合适就旋转这个词。 意思就是词云的算法水平词和竖直方向词的一种数量衡量。

mask :控制词云的背景。nd-array or None (default=None)如果是空,就使用width和height参数。不然使用mask作为背景。

scale:缩放图片

max_words:显示的最大词

stopwords:停用词

relative_scaling:这个比较有意思,如果true,字体的大小与词语顺序有关。false,字体大小与词云频率有关。

相关函数

fit_words(frequencies) 根据单词与频率生成词云

generate(tex) 根据文本直接生成词云,仅限 英文的

generate_from_frequencies(frequencies, max_font_size=None) 根据单词与频率生成词云,可以指定最大数目

generate_from_text()根据文本直接生成词云。英文的

process_text(text) 根据text生成单词的统计数目,返回{word,int},去除了停用词。只限于英文的

关于fit_words的参数问题,

20180526112040492929.png

让我们传的是一个tuple,包含了word和frequency,实际我这么做的时候参数是错误了,看一下源代码

def fit_words(self, frequencies):

"""Create a word_cloud from words and frequencies.

Alias to generate_from_frequencies.

Parameters

----------

frequencies : array of tuples

A tuple contains the word and its frequency.

Returns

-------

self

"""

return self.generate_from_frequencies(frequencies)

参数说明仍然说是用“ A tuple contains the word and its frequency.”,又去调用了self.generate_from_frequencies(frequencies),

def generate_from_frequencies(self, frequencies, max_font_size=None):

"""Create a word_cloud from words and frequencies.

Parameters

----------

frequencies : dict from string to float

A contains words and associated frequency.

max_font_size : int

Use this font-size instead of self.max_font_size

Returns

-------

self

"""

# make sure frequencies are sorted and normalized

frequencies = sorted(frequencies.items(), key=item1, reverse=True)

frequencies = frequencies[:self.max_words]

# largest entry will be 1

max_frequency = float(frequencies[0][4])

frequencies = [(word, freq / max_frequency)

for word, freq in frequencies]

这个时候的参数成了"dict from string to float",而且里面那个列表生成式 相当于生成了一个新的 frequencies ,这个新的 frequencies 是个array of tuple。所以我们还是要传字典形式的。只不过函数内部变成了 array of tuple 。。。。。年久未修?

3. 知乎教育水平 生成词云实例

#coding=utf-8

#导入wordcloud模块和matplotlib模块

from wordcloud import WordCloud,ImageColorGenerator

import matplotlib.pyplot as plt

from scipy.misc import imread

import jieba

import jieba.analyse

content = (",").join(data2[‘教育经历‘].values.tolist())#dataframe格式数据

tags = jieba.analyse.extract_tags(content, topK=200, withWeight=False)

text =" ".join(tags)

print(tags)

#读入背景图片

bj_pic=imread(‘1.png‘)

#生成词云(通常字体路径均设置在C:\\Windows\\Fonts\\也可自行下载)

font=r‘C:\\Windows\\Fonts\\STFANGSO.ttf‘#不加这一句显示口字形乱码 ""报错

wordcloud=WordCloud(mask=bj_pic,background_color=‘white‘,font_path=font,scale=0.5).generate_from_text(text) #直接根据文本生成 词云

plt.imshow(wordcloud)

plt.axis(‘off‘)

plt.show()

wordcloud.to_file(‘test2.jpg‘)

20180526112040522224.png

可以试着停用词,university,或者直接在tags里把不想要的删除。图片很小,用了scale=0.5

20180526112040615968.png

尝试了一下fit_words,必须传入字典形式的。!!!

wordcloud = WordCloud(mask=bj_pic,background_color=‘white‘,font_path=font,scale=3.5).fit_words({"sb":3,"我是":4,"操":10})

plt.imshow(wordcloud)

plt.axis(‘off‘)

plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值