-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmine.py
53 lines (45 loc) · 1.29 KB
/
mine.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'''
Author: Jaakko Lappalainen, 2013. email: [email protected]
'''
''' Some scripts to mine data. Just select the tag you are interested in and
the code counts the ocurrences. '''
####### Gets values from key in a json recursively
import json
def get_all(myjson, key, result=None):
result = [] if result is None else result
if type(myjson) in (str,unicode):
myjson = json.loads(myjson)
if type(myjson) is dict:
for jsonkey in myjson.keys():
if type(myjson[jsonkey]) in (list, dict):
get_all(myjson[jsonkey], key, result)
elif jsonkey == key:
result.append(myjson[jsonkey])
elif type(myjson) is list:
for item in myjson:
if type(item) in (list, dict):
get_all(item, key, result)
############################################
# common 'attributeName'-s
rawdata = []
for d in db.all.find():
get_all(d,'dataset',rawdata)
data = {}
for d in rawdata:
try:
data[d]+=1
except:
data[d]=1
rawdata=data
for d in rawdata.keys():
if rawdata[d] < 10:
del rawdata[d]
names = rawdata.keys()
data = np.array(rawdata.values())
ax = plt.subplot(111)
width=1
bins = map(lambda x: x-width/2,range(1,len(data)+1))
ax.bar(bins,data,width=width)
ax.set_xticks(map(lambda x: x, range(1,len(data)+1)))
ax.set_xticklabels(names,rotation=90, rotation_mode="anchor", ha="right")
plt.show()