Skip to content

Commit 99e3725

Browse files
committed
Update 03-analytics-01.md
1 parent bb0f7dd commit 99e3725

File tree

1 file changed

+243
-1
lines changed

1 file changed

+243
-1
lines changed

chapters/03-analytics-01.md

Lines changed: 243 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,243 @@
1-
#Github项目分析一
1+
#Github项目分析一
2+
3+
#用matplotlib生成图表
4+
5+
如何分析用户的数据是一个有趣的问题,特别是当我们有大量的数据的时候。
6+
除了``matlab``,我们还可以用``numpy``+``matplotlib``
7+
8+
##python github用户数据分析##
9+
10+
数据可以在这边寻找到
11+
12+
[https://github.com/gmszone/ml](https://github.com/gmszone/ml)
13+
14+
最后效果图
15+
<img src="https://raw.githubusercontent.com/gmszone/ml/master/screenshots/2014-01-01.png" width=600>
16+
17+
要解析的json文件位于``data/2014-01-01-0.json``,大小6.6M,显然我们可能需要用每次只读一行的策略,这足以解释为什么诸如sublime打开的时候很慢,而现在我们只需要里面的json数据中的创建时间。。
18+
19+
==
20+
这个文件代表什么?
21+
22+
**2014年1月1日零时到一时,用户在github上的操作,这里的用户指的是很多。。一共有4814条数据,从commit、create到issues都有。**
23+
24+
##python json文件解析##
25+
26+
import json
27+
for line in open(jsonfile):
28+
line = f.readline()
29+
30+
然后再解析json
31+
<pre><code class="python">
32+
import dateutil.parser
33+
34+
lin = json.loads(line)
35+
date = dateutil.parser.parse(lin["created_at"])
36+
</code></pre>
37+
这里用到了``dateutil``,因为新鲜出炉的数据是string需要转换为``dateutil``,再到数据放到数组里头。最后有就有了``parse_data``
38+
39+
def parse_data(jsonfile):
40+
f = open(jsonfile, "r")
41+
dataarray = []
42+
datacount = 0
43+
44+
for line in open(jsonfile):
45+
line = f.readline()
46+
lin = json.loads(line)
47+
date = dateutil.parser.parse(lin["created_at"])
48+
datacount += 1
49+
dataarray.append(date.minute)
50+
51+
minuteswithcount = [(x, dataarray.count(x)) for x in set(dataarray)]
52+
f.close()
53+
return minuteswithcount
54+
55+
56+
下面这句代码就是将上面的解析为
57+
58+
minuteswithcount = [(x, dataarray.count(x)) for x in set(dataarray)]
59+
60+
这样的数组以便于解析
61+
62+
[(0, 92), (1, 67), (2, 86), (3, 73), (4, 76), (5, 67), (6, 61), (7, 71), (8, 62), (9, 71), (10, 70), (11, 79), (12, 62), (13, 67), (14, 76), (15, 67), (16, 74), (17, 48), (18, 78), (19, 73), (20, 89), (21, 62), (22, 74), (23, 61), (24, 71), (25, 49), (26, 59), (27, 59), (28, 58), (29, 74), (30, 69), (31, 59), (32, 89), (33, 67), (34, 66), (35, 77), (36, 64), (37, 71), (38, 75), (39, 66), (40, 62), (41, 77), (42, 82), (43, 95), (44, 77), (45, 65), (46, 59), (47, 60), (48, 54), (49, 66), (50, 74), (51, 61), (52, 71), (53, 90), (54, 64), (55, 67), (56, 67), (57, 55), (58, 68), (59, 91)]
63+
64+
##matplotlib##
65+
开始之前需要安装``matplotlib
66+
67+
sudo pip install matplotlib
68+
69+
然后引入这个库
70+
71+
import matplotlib.pyplot as plt
72+
73+
如上面的那个结果,只需要
74+
75+
<pre><code class="python">
76+
plt.figure(figsize=(8,4))
77+
plt.plot(x, y,label = files)
78+
plt.legend()
79+
plt.show()
80+
</code></pre>
81+
82+
最后代码可见
83+
84+
#!/usr/bin/env python
85+
# -*- coding: utf-8 -*-
86+
87+
import json
88+
import dateutil.parser
89+
import numpy as np
90+
import matplotlib.mlab as mlab
91+
import matplotlib.pyplot as plt
92+
93+
94+
def parse_data(jsonfile):
95+
f = open(jsonfile, "r")
96+
dataarray = []
97+
datacount = 0
98+
99+
for line in open(jsonfile):
100+
line = f.readline()
101+
lin = json.loads(line)
102+
date = dateutil.parser.parse(lin["created_at"])
103+
datacount += 1
104+
dataarray.append(date.minute)
105+
106+
minuteswithcount = [(x, dataarray.count(x)) for x in set(dataarray)]
107+
f.close()
108+
return minuteswithcount
109+
110+
111+
def draw_date(files):
112+
x = []
113+
y = []
114+
mwcs = parse_data(files)
115+
for mwc in mwcs:
116+
x.append(mwc[0])
117+
y.append(mwc[1])
118+
119+
plt.figure(figsize=(8,4))
120+
plt.plot(x, y,label = files)
121+
plt.legend()
122+
plt.show()
123+
124+
draw_date("data/2014-01-01-0.json")
125+
126+
127+
#每周分析
128+
129+
继上篇之后,我们就可以分析用户的每周提交情况,以得出用户的真正的工具效率,每个程序员的工作时间可能是不一样的,如
130+
![Phodal Huang's Report][1]
131+
132+
[1]: https://www.phodal.com/static/media/uploads/screen_shot_2014-04-12_at_9.58.52_am.png
133+
134+
这是我的每周情况,显然如果把星期六移到前面的话,随着工作时间的增长,在github上的使用在下降,作为一个
135+
136+
a fulltime hacker who works best in the evening (around 8 pm).
137+
138+
不过这个是osrc的分析结果。
139+
140+
##python github 每周情况分析##
141+
142+
看一张分析后的结果
143+
144+
<img src="https://raw.githubusercontent.com/gmszone/ml/master/screenshots/feb-results.png" width=600>
145+
146+
结果正好与我的情况相反?似乎图上是这么说的,但是数据上是这样的情况。
147+
148+
data
149+
├── 2014-01-01-0.json
150+
├── 2014-02-01-0.json
151+
├── 2014-02-02-0.json
152+
├── 2014-02-03-0.json
153+
├── 2014-02-04-0.json
154+
├── 2014-02-05-0.json
155+
├── 2014-02-06-0.json
156+
├── 2014-02-07-0.json
157+
├── 2014-02-08-0.json
158+
├── 2014-02-09-0.json
159+
├── 2014-02-10-0.json
160+
├── 2014-02-11-0.json
161+
├── 2014-02-12-0.json
162+
├── 2014-02-13-0.json
163+
├── 2014-02-14-0.json
164+
├── 2014-02-15-0.json
165+
├── 2014-02-16-0.json
166+
├── 2014-02-17-0.json
167+
├── 2014-02-18-0.json
168+
├── 2014-02-19-0.json
169+
└── 2014-02-20-0.json
170+
171+
我们获取是每天晚上0点时的情况,至于为什么是0点,我想这里的数据量可能会比较少。除去1月1号的情况,就是上面的结果,在只有一周的情况时,总会以为因为在国内那时是假期,但是总觉得不是很靠谱,国内的程序员虽然很多,会在github上活跃的可能没有那么多,直至列出每一周的数据时。
172+
173+
6570, 7420, 11274, 12073, 12160, 12378, 12897,
174+
8474, 7984, 12933, 13504, 13763, 13544, 12940,
175+
7119, 7346, 13412, 14008, 12555
176+
177+
##python 数据分析##
178+
179+
重写了一个新的方法用于计算提交数,直至后面才意识到其实我们可以算行数就够了,但是方法上有点hack
180+
181+
<pre><code class="python">
182+
def get_minutes_counts_with_id(jsonfile):
183+
datacount, dataarray = handle_json(jsonfile)
184+
minuteswithcount = [(x, dataarray.count(x)) for x in set(dataarray)]
185+
return minuteswithcount
186+
187+
188+
def handle_json(jsonfile):
189+
f = open(jsonfile, "r")
190+
dataarray = []
191+
datacount = 0
192+
193+
for line in open(jsonfile):
194+
line = f.readline()
195+
lin = json.loads(line)
196+
date = dateutil.parser.parse(lin["created_at"])
197+
datacount += 1
198+
dataarray.append(date.minute)
199+
200+
f.close()
201+
return datacount, dataarray
202+
203+
204+
def get_minutes_count_num(jsonfile):
205+
datacount, dataarray = handle_json(jsonfile)
206+
return datacount
207+
208+
209+
def get_month_total():
210+
"""
211+
212+
:rtype : object
213+
"""
214+
monthdaycount = []
215+
for i in range(1, 20):
216+
if i < 10:
217+
filename = 'data/2014-02-0' + i.__str__() + '-0.json'
218+
else:
219+
filename = 'data/2014-02-' + i.__str__() + '-0.json'
220+
monthdaycount.append(get_minutes_count_num(filename))
221+
return monthdaycount
222+
</code></pre>
223+
接着我们需要去遍历每个结果,后面的后面会发现这个效率真的是太低了,为什么木有多线程?
224+
225+
##python matplotlib图表##
226+
让我们的matplotlib来做这些图表的工作
227+
228+
if __name__ == '__main__':
229+
results = pd.get_month_total()
230+
print results
231+
232+
plt.figure(figsize=(8, 4))
233+
plt.plot(results.__getslice__(0, 7), label="first week")
234+
plt.plot(results.__getslice__(7, 14), label="second week")
235+
plt.plot(results.__getslice__(14, 21), label="third week")
236+
plt.legend()
237+
plt.show()
238+
239+
蓝色的是第一周,绿色的是第二周,蓝色的是第三周就有了上面的结果。
240+
241+
我们还需要优化方法,以及多线程的支持。
242+
243+

0 commit comments

Comments
 (0)