wordcloud使用
学了下怎么用wordcloud。
以imet的数据集为例
https://www.kaggle.com/c/imet-2019-fgvc6
读取“train.csv”,”label.csv”文件,得到id2name[] (label的id和label名称对应) 和 attribute_count(label出现次数统计)两个dict。
import matplotlib.pyplot as plt
import numpy as np import osimport csv lines=csv.reader(open("train.csv"))
train_content = []
head_row =next(lines)
for line in lines:
train_content.append(line) attribute_ids = []
for line in train_content:
attributes = line[1].split()
for a in attributes:
attribute_ids.append(a) lines=csv.reader(open("labels.csv"))
attribute_content = []
head_row =next(lines)
for line in lines:
attribute_content.append(line)
id2name = {}
for line in attribute_content:
if line[0] not in id2name:
id2name.update({line[0]:line[1]})
def count_list(lt):
d={}
for i in lt:
if (i in d.keys()):
continue
count = lt.count(i)
d[i] = count
return d
attribute_count = count_list(attribute_ids)
对attribute_count进行排序,输出出现次数较多的标签(前十个)
sorted_attribute= sorted(attribute_count.items(),key = lambda item :item[1],reverse = True)
for i in range(10):
print (sorted_attribute[i][0],': ',id2name[sorted_attribute[i][0]])
print (sorted_attribute[i][1])
结果为

然而这样还不够直观,使用wordcloud可以更直观展示词频。
需要的python库
seaborn、wordcloud
准备好dict
culture_count_dict = {}
tag_count_dict = {}
for i in range(1103):
    idx = str(i)
    if (id2name[idx][0:5] == 'tag::'):
        tag_count_dict.update({id2name[idx][5:]:attribute_count[idx]})
    else:
        culture_count_dict.update({id2name[idx][9:]:attribute_count[idx]})
wordcloud 生成图像
import seaborn as sns
from wordcloud import WordCloud culture_cloud = WordCloud(background_color='Black', colormap='Paired', width=1600, height=800, random_state=123).generate_from_frequencies(culture_count_dict)
tag_cloud = WordCloud(background_color='Black', colormap='Paired', width=1600, height=800, random_state=123).generate_from_frequencies(tag_count_dict) plt.figure(figsize=(24,24))
plt.subplot(211)
plt.imshow(culture_cloud,interpolation='bilinear')
plt.axis('off') plt.subplot(212)
plt.imshow(tag_cloud, interpolation='bilinear')
plt.axis('off') plt.tight_layout()
plt.show()


wordcloud使用的更多相关文章
- python wordcloud 对电影《我不是潘金莲》制作词云
		
上个星期五(16/11/18)去看了冯小刚的最新电影<我不是潘金莲>,电影很长,有点黑色幽默.看完之后我就去知乎,豆瓣电影等看看大家对于这部电影的评价.果然这是一部很有争议的电影,无论是在 ...
 - R语言之词云:wordcloud&wordcloud2安装及参数说明
		
一.wordcloud安装说明 install.packages("wordcloud"); 二.wordcloud2安装说明 install.packages("dev ...
 - python wordcloud
		
python wordcloud 对电影<我不是潘金莲>制作词云 上个星期五(16/11/18)去看了冯小刚的最新电影<我不是潘金莲>,电影很长,有点黑色幽默.看完之后我就去知 ...
 - [R] Draw a wordcloud
		
# 加载rJava.Rwordseg库 library(rJava); library(Rwordseg); library(RColorBrewer); # == 读入数据 lecture=read ...
 - Rweibo , wordcloud
		
利用Rweibo ,wordcloud做词云 #导入需要的包,不存在则下载 require(Rweibo) #必须先调用rJava不然Rwordseg 无法使用 library(rJava) requ ...
 - 使用 wordcloud 构建词云图
		
from wordcloud import WordCloudfrom matplotlib import pyplot as pltfrom PIL import Imageimport numpy ...
 - 爬取豆瓣电影影评,生成wordcloud词云,并利用监督学习根据评论自动打星
		
本文的完整源码在git位置:https://github.com/OceanBBBBbb/douban-ml 爬取豆瓣影评 爬豆瓣的影评比较简单,豆瓣没有做限制,甚至你都不用登陆就可以看全部,我这里用 ...
 - 使用jieba库与wordcloud库第三方库进行词频统计
		
一.jieba库与wordcloud库的使用 1.jieba库与wordcloud库的介绍 jieba 库的分词原理是利用一个中文词库,将待分词的内容与分词词库进行比对,通过图结构和动态规划方法找到最 ...
 - win 10 的wordcloud的安装
		
这两天为了安装wordcloud库可谓是“一把辛酸”,各种出错 jieba什么就不说了,安装和使用都很简单只需要一句代码就可以实现了,而wordcloud在安装之前,本以为也像jieba那样的简单,但 ...
 - 词云wordcloud入门示例
		
整体简介: 词云图,也叫文字云,是对文本中出现频率较高的“关键词”予以视觉化的展现,词云图过滤掉大量的低频低质的文本信息,使得浏览者只要一眼扫过文本就可领略文本的主旨. 基于Python的词云生成类库 ...
 
随机推荐
- SQL Server 2005 系统数据介绍:dm_exec_connections
			
原文:SQL Server 2005 系统数据介绍:dm_exec_connections 转载:http://msdn.microsoft.com/zh-cn/library/ms181509(SQ ...
 - hdata datax交流总结
			
http://blog.csdn.net/zlm838687/article/details/74781522 hdata datax交流总结 今天和阿里云的同学就数据同步做了简要的交流,下面就交流的 ...
 - 一分钟sed入门
			
[转载于58同城沈剑] 1.简介 sed是一种行编辑器,它一次处理一行内容. 2.sed调用方式 sed [options] 'command' file(s) sed [options] -f sc ...
 - java根据身份证号和获取用户年龄和性别的工具类
			
import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util ...
 - ECSHOP筛选属性修改title标题
			
发现百度蜘蛛爬行网站时会爬行属性链接,而且会进行收录.可是ecshop系统制作的网站,在分类页点击属性筛选出产品时,网页title不会改变.这样就会造成大量title一样的页面,不利于优化.为此,在网 ...
 - [Debug] Diagnose a Slow Page Using Developer Tools
			
Your page is showing high CPU usage and spinning up your laptop fan, but nothing is visibly happenin ...
 - EffectiveJava(6)消除过期对象的引用
			
消除过期对象的引用 过期引用 – 在操作集合类的时候,未清理过期的对象引用(即:过期引用),常会导致内存泄露.从而报outOfmemory错误. 1.过期对象导致内存泄漏. 2.一旦对象引用过期,清除 ...
 - SurfaceView实现拍照预览
			
一.布局代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro ...
 - HDU1789 Doing Homework again 【贪心】
			
Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
 - C语言结构体及函数传递数组參数演示样例
			
注:makeSphere()函数返回Sphere结构体,main函数中.调用makeSphere()函数,传递的第一个參数为数组,传递的数组作为指针.