.show() fails on RandomForestClassifier model

User 2032 | 8/12/2015, 1:38:29 PM

Hi Guys,

I've managed to train a random forest model (trains succesfull and works on withheld examples). However I wanted to have a look at features it uses for classifications, but when I called model.show() I got the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-51-62181d60d3de> in <module>()
----> 1 m.show()

/home/production/.virtualenvs/tailor_core/lib/python2.7/site-packages/graphlab/toolkits/classifier/random_forest_classifier.pyc in show(self, view, tree_id, vlabel_hover)
    213             return self._show_tree(tree_id=tree_id, vlabel_hover=vlabel_hover)
--> 215         return super(_Classifier, self).show(obj=self)
    217     def _show_tree(self, tree_id=0, vlabel_hover=False):

TypeError: show() got an unexpected keyword argument 'obj'


User 1359 | 8/12/2015, 5:43:46 PM

Hey Johnny,

This is definitely a bug in GLC. I have reported it and it will be fixed in the next release. Thanks so much for bringing this to our attention.

In the meantime, you can use this workaround for using Canvas with the Random Forest Classifier

super(graphlab.toolkits._supervised_learning.Classifier, model).show()

Thanks! Dick

User 2032 | 8/12/2015, 6:17:02 PM

this will not work either because the tree is too big (btw. tree visualization is probably the worst feature in canvas - there are so much better ways to visualize trees (once you can assume it is a tree - see http://bl.ocks.org/ashenfad/5234819 for example)). The true work around is (assuming m is where you saved the model; you can adjust depth untill the visualization fits):

from graphlab.toolkits._internal_utils import _SGraphFromJsonTree
tree_id = 0
tree_json = m.get('trees_json')[tree_id]
g = _SGraphFromJsonTree(tree_json).get_neighborhood([0,1], depth)
vlabel_hover = False
# Make the label for each vertex based on its type
def get_vlabel(record):
    if record['type'] == 'leaf':
        return str(record['value'])
    elif record['type'] == 'indicator':
        return str(record['name'])
        return str(record['name']) + '<' + str(record['value'])
g.vertices['__repr__'] = g.vertices.apply(get_vlabel, str)

fuchsia = [0.69, 0., 0.48]
blue = [0.039, 0.55, 0.77]
orange = [1.0, 0.33, 0.]
green = [0.52, 0.74, 0.]

# Make the color for leaf nodes
def get_leaf_vcolor(record):
    '''assign color to leaf vertex, negative_leaf->blue, positive_leaf->orange'''
    return blue if record['value'] < 0 else orange

leaf_vertices = g.vertices[g.vertices['type'] == 'leaf']
root_vertex_color = {0: fuchsia}
leaf_vertex_color = dict([(x['__id'], get_leaf_vcolor(x)) for x in leaf_vertices])
highlight = {}
elabel = 'value' if len(g.edges) > 0 else None

# Hack: we want the canvas to show tree_{i} instead of g, so here is how we do it.
graph_name = 'tree_' + str(tree_id)
locals().update({graph_name: g})
del g
locals()[graph_name].show(vlabel='__repr__', elabel=elabel,
                          highlight=highlight, arrows=True)