于贵洋的博客

BI、数据分析


  • 首页

  • 分类

  • 标签

  • 归档

  • 站点地图

  • 公益404

  • 关于

  • 搜索

Python中文分词-jieba

发表于 2017-08-19 | 分类于 Python-基础

刚刚在看Python的词云图,想要显示中文的时候,需要做一个分词,这里我们学习下jieba分词。

1. jieba中文分词

jieba是Python中文分词的一个组件
github地址:[https://github.com/fxsjy/jieba](https://github.com/fxsjy/jieba)

支持三种分词模式:精确模式,试图将句子最精确地切开,适合文本分析;全模式,把句子中所有的可以成词的词语都扫描出来, 速度非常快,但是不能解决歧义;搜索引擎模式,在精确模式的基础上,对长词再次切分,提高召回率,适合用于搜索引擎分词。支持繁体分词支持自定义词典MIT 授权协议

安装:

1
pip install jieba

官网上的小实例

1
2
3
4
5
6
7
8
9
10
11
12
13
import jieba
seg_list = jieba.cut("我来到北京清华大学", cut_all=True)
print("Full Mode: " + "/ ".join(seg_list)) # 全模式
seg_list = jieba.cut("我来到北京清华大学", cut_all=False)
print("Default Mode: " + "/ ".join(seg_list)) # 精确模式
seg_list = jieba.cut("他来到了网易杭研大厦") # 默认是精确模式
print(", ".join(seg_list))
seg_list = jieba.cut_for_search("小明硕士毕业于中国科学院计算所,后在日本京都大学深造") # 搜索引擎模式
print(", ".join(seg_list))

1
2
3
4
5
6
7
8
Building prefix dict from the default dictionary ...
Dumping model to file cache C:\Users\YUGUIY~1\AppData\Local\Temp\jieba.cache
Loading model cost 0.884 seconds.
Prefix dict has been built succesfully.
Full Mode: 我/ 来到/ 北京/ 清华/ 清华大学/ 华大/ 大学
Default Mode: 我/ 来到/ 北京/ 清华大学
他, 来到, 了, 网易, 杭研, 大厦
小明, 硕士, 毕业, 于, 中国, 科学, 学院, 科学院, 中国科学院, 计算, 计算所, ,, 后, 在, 日本, 京都, 大学, 日本京都大学, 深造

到这里,想要的功能其实已经足够了,官网还有很多其他的介绍内容,很强大,后面需要的时候,再来补充,接着回去改那个词云的了先。

word_cloud-用Python之作个性化词云图

发表于 2017-08-19 | 分类于 数据可视化-Python&R

网上有很多制作词云的网站,我们使用Python也可以很方便的制作,这里,我们就简单学习下。

1. word_cloud

GitHub地址:https://github.com/amueller/word_cloud
首先我们需要安装,正常来说,直接就执行

1
pip install wordcloud

即可,但是,我这个是在Windows平台,安装的时候,提示什么少了,需要去下载个编译器的,报错信息后面有URL,

这里忘记记下来了,遇到的同学,直接去下载下应该就行了

error: Microsoft Visual C++ 14.0 is required

这里,我从网上找了另一个方法解决
去这个网站:http://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud
找到指定的版本下载就行了

阅读全文 »

Python基础(9)- 排序技巧

发表于 2017-08-18 | 分类于 Python-基础

通常,我们排序的时候,我们可以使用系统内置的sorted函数或list自带的sort函数。
参考文章:http://www.jb51.net/article/57678.htm
https://docs.python.org/3/library/stdtypes.html?highlight=sort#list.sort

1. list.sort

sort(*, key=None, reverse=False)This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).

sort是就地排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
list1 = [66, 48, 28, 69, 86, 96, 89, 37, 56, 33]
list1
Out[93]: [66, 48, 28, 69, 86, 96, 89, 37, 56, 33]
list1.sort()
list1
Out[95]: [28, 33, 37, 48, 56, 66, 69, 86, 89, 96]
#默认是升序,可以使用reverse降序
list1.sort(reverse=True)
list1
Out[97]: [96, 89, 86, 69, 66, 56, 48, 37, 33, 28]

key可以传入一个函数,会作用于每一个element,返回值会作为排序键

key specifies a function of one argument that is used to extract a comparison key from each list element (for example, key=str.lower). The key corresponding to each item in the list is calculated once and then used for the entire sorting process. The default value of None means that list items are sorted directly without calculating a separate key value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
list2=list('AckjehLhct')
list2
Out[99]: ['A', 'c', 'k', 'j', 'e', 'h', 'L', 'h', 'c', 't']
list2.sort()
list2
Out[101]: ['A', 'L', 'c', 'c', 'e', 'h', 'h', 'j', 'k', 't']
list2.sort(key=str.lower)
list2
Out[103]: ['A', 'c', 'c', 'e', 'h', 'h', 'j', 'k', 'L', 't']

2. sorted

sorted(iterable, *, key=None, reverse=False)Return a new sorted list from the items in iterable

sorted是返回一个排序后的新list,使用方式基本一样,只是

1
2
3
4
5
6
7
8
9
10
list3 = [84, 57, 15, 84, 40, 37, 66, 45, 11, 95]
list3
Out[107]: [84, 57, 15, 84, 40, 37, 66, 45, 11, 95]
sorted(list3)
Out[108]: [11, 15, 37, 40, 45, 57, 66, 84, 84, 95]
sorted(list3,reverse=True)
Out[109]: [95, 84, 84, 66, 57, 45, 40, 37, 15, 11]

sorted方法对所有的可迭代对象都有效

1
2
sorted({3:'a',1:'z',5:'b',2:'c'})
Out[110]: [1, 2, 3, 5]

使用sorted还能对复杂对象进行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
l_op=[('namei',20,'a'),('lufei',19,'c'),('qiaoba',15,'b')]
l_op
Out[112]: [('namei', 20, 'a'), ('lufei', 19, 'c'), ('qiaoba', 15, 'b')]
#默认是按照每一个tuple的第一个元素来排序的
sorted(l_op)
Out[113]: [('lufei', 19, 'c'), ('namei', 20, 'a'), ('qiaoba', 15, 'b')]
sorted(l_op,key=lambda x: x[0])
Out[114]: [('lufei', 19, 'c'), ('namei', 20, 'a'), ('qiaoba', 15, 'b')]
#我们可以通过key函数,来指定要使用的排序键,
sorted(l_op,key=lambda x: x[1])
Out[115]: [('qiaoba', 15, 'b'), ('lufei', 19, 'c'), ('namei', 20, 'a')]
sorted(l_op,key=lambda x: x[2],reverse=True)
Out[116]: [('lufei', 19, 'c'), ('qiaoba', 15, 'b'), ('namei', 20, 'a')]
sorted(l_op,key=lambda x: x[2])
Out[117]: [('namei', 20, 'a'), ('qiaoba', 15, 'b'), ('lufei', 19, 'c')]

对于自定义的对象也是可以的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class People:
def __init__(self,id,name):
self.id=id
self.name=name
p1 = People(10,'lufei')
p2 = People(5,'namei')
p3 = People(15,'qiaoba')
#指定我们要排序的字段就行了
for p in sorted([p1,p2,p3],key=lambda p: p.id):
print(p.id,p.name)
#out:
5 namei
10 lufei
15 qiaoba
for p in sorted([p1,p2,p3],key=lambda p: p.name):
print(p.id,p.name)
#out:
10 lufei
5 namei
15 qiaoba

matplotlib手册(10)-用pyplot实现“房间里100个人玩游戏的例子”

发表于 2017-08-18 | 分类于 数据可视化-Python&R

之前有篇文章,说房间里有100个人,每人100块钱,的那个
原文介绍:用数据分析告诉你这个世界很有意思

觉得挺有意思的,昨天发现pyplot也可以绘制动画,就来试试,主要是实现动画效果,其他的暂时先不考虑了
目前跑起来是可以,就是比较慢,还没找到原因
整体想法,就是
x轴表示玩家的序号,财富值用y轴来表示

阅读全文 »

matplotlib手册(9) - 绘制动画

发表于 2017-08-17 | 分类于 数据可视化-Python&R

matplotlib手册(9)

绘制动画

前面,我们介绍了很多绘图的方法,matplotlib不单单可以绘制静态的图,还可以制作动态的图,下面,我们就来学习下。

我们主要参考matplotlib官网的例子http://matplotlib.org/api/animation_api.html

创建动画最简单的方式,就是使用Animation的子类,就是下面的这2个

1. FuncAnimation

函数介绍及主要参数

1
2
3
4
5
6
7
8
9
10
11
class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, **kwargs)
fig : matplotlib.figure.Figure
func : callable
frames : iterable, int, generator function, or None, optional
init_func : callable, optional
fargs : tuple or None, optional
interval : number, optional
repeat_delay : number, optional
repeat : bool, optional

阅读全文 »
1…91011…23
于贵洋

于贵洋

111 日志
17 分类
30 标签
RSS
GitHub
友情链接
  • 很久之前的CSDN博客
0%
© 2017 于贵洋
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.3
Hosted by GitHub Pages
本站访客数 人次 本站总访问量 次