Skip to content

Commit a3df6b8

Browse files
committed
Add simple Python generation
1 parent 137f941 commit a3df6b8

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

bigml/tree.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@
2525
api = BigML()
2626
2727
model = api.get_model('model/5026965515526876630001b2')
28+
model = api.get_model('model/5026a3c315526876630001b5')
2829
2930
tree = Tree(model['object']['model']['root'], model['object']['model']['fields'], model['object']['objective_fields'])
3031
tree.predict({"000002": 2.46, "000003": 1})
3132
tree.rules()
33+
tree.python()
3234
3335
"""
3436
import logging
3537
LOGGER = logging.getLogger('BigML')
3638

3739
import operator
40+
import unidecode
41+
import re
3842

3943
OPERATOR = {
4044
"<": operator.lt,
@@ -51,11 +55,16 @@ def __init__(self, operator, field, value):
5155
self.field = field
5256
self.value = value
5357

58+
def slugify(str):
59+
str = unidecode.unidecode(str).lower()
60+
return re.sub(r'\W+', '_', str)
61+
5462
class Tree(object):
5563

5664
def __init__(self, tree, fields, objective_field=None):
5765

5866
self.fields = fields
67+
5968
if objective_field and isinstance(objective_field, list):
6069
self.objective_field = objective_field[0]
6170
else:
@@ -72,7 +81,7 @@ def __init__(self, tree, fields, objective_field=None):
7281
children = []
7382
if 'children' in tree:
7483
for child in tree['children']:
75-
children.append(Tree(child, fields, objective_field))
84+
children.append(Tree(child, self.fields, objective_field))
7685
self.children = children
7786
self.count = tree['count']
7887
self.distribution = tree['distribution']
@@ -112,3 +121,33 @@ def rules(self, depth=0):
112121
' ' * depth,
113122
self.fields[self.objective_field]['name'] if self.objective_field else "Prediction",
114123
self.output))
124+
125+
126+
def python_body(self, depth=1):
127+
if self.children:
128+
for child in self.children:
129+
print("%sif (%s %s %s)%s" %
130+
(' ' * depth,
131+
self.fields[child.predicate.field]['slug'],
132+
child.predicate.operator,
133+
child.predicate.value,
134+
":" if child.children else ":"))
135+
child.python_body(depth+1)
136+
else:
137+
if self.fields[self.objective_field]['optype'] == 'numeric':
138+
print("%s return %s" % (' ' * depth, self.output))
139+
else:
140+
print("%s return '%s'" % (' ' * depth, self.output))
141+
142+
def python(self):
143+
args = []
144+
for key in self.fields.iterkeys():
145+
slug = slugify(self.fields[key]['name'])
146+
self.fields[key].update(slug=slug)
147+
if key != self.objective_field:
148+
args.append(slug)
149+
print("def predict_%s(%s):" % (self.fields[self.objective_field]['slug'], ", ".join(args)))
150+
self.python_body()
151+
152+
153+

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
setup_requires = [],
5757
packages = ['bigml'],
5858
include_package_data = True,
59-
install_requires = ['requests'],
59+
install_requires = ['requests', 'poster', 'unidecode'],
6060
classifiers=[
6161
'Development Status :: 4 - Beta',
6262
'Intended Audience :: Developers',

0 commit comments

Comments
 (0)