-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathweight_model_extract.py
44 lines (31 loc) · 1.16 KB
/
weight_model_extract.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
import operator
from jubakit.model import JubaModel
"""
Extracting Model Contents
========================================
This is an example to show the usage of ``jubakit.model`` package,
which allows low-level model manipulation.
To try this example, first save a model file of jubaweight.
(hint: ``weight_shogun.py`` example automatically saves the model under /tmp)
Then run this example like:
$ python weight_model_extract.py /tmp/127.0.0.1_0000_weight_shogun.jubatus
to see the term frequency of each feature vector.
"""
# Load the model file.
modelpath = 'weight_shogun_model.jubatus'
if 1 < len(sys.argv):
modelpath = sys.argv[1]
with open(modelpath, 'rb') as f:
model = JubaModel.load_binary(f)
# Extract the term frequency part of the model data.
weights = model.data()[0][1][1][0]
# Sort features by the term frequency.
sorted_weights = sorted(weights.items(), key=operator.itemgetter(1), reverse=True)
# Print the result.
print("Weight\t\tFeature")
for (k, v) in sorted_weights:
print("{0}\t\t{1}".format(v, k))