Search code examples
python-3.xtreeexpression-treesgraph-visualization

export_graphviz extratreesclassifier python visualization


Is there a way to use export_graphviz to represent the final tree of extratreesclasiffier? If not, how can it be visually representented?


Solution

  • I think there is no way to visualize a specific tree from sklearn.ensemble.ExtraTreesClassifier.

    But you can visualize it with a single tree from sklearn.tree.ExtraTreeClassifier.

    from sklearn import tree
    ...
    
    #Fit an Extra Tree model to the data
    model = tree.ExtraTreeClassifier()
    model.fit(X_train, y_train)
    
    #Use http://webgraphviz.com to visualize the graph of this file
    with open("tree_classifier.txt", "w") as f:
        f = tree.export_graphviz(model, out_file=f)
    

    Here an example with Decision tree that also work with Extra tree: http://dataaspirant.com/2017/04/21/visualize-decision-tree-python-graphviz/