How to visualize a Regression Tree in Python

前端 未结 2 1021
天涯浪人
天涯浪人 2021-01-07 08:26

I\'m looking to visualize a regression tree built using any of the ensemble methods in scikit learn (gradientboosting regressor, random forest regressor,bag

相关标签:
2条回答
  • 2021-01-07 08:50

    After much searching, I found software offered by Turi that models a regression tree, not to be confused with a decision tree. Hope this helps

    For what its worth, a regression tree looks like this:

    While a decision/classifier tree looks like this:

    And tho they look the same, the attribute needed to create this is tree_ which is only available to classifiers, not regressors.

    0 讨论(0)
  • 2021-01-07 08:57

    As I commented, there is no functional difference between a classification and a regression decision tree plot. Adapting the regression toy example from the docs:

    from sklearn import tree
    X = [[0, 0], [2, 2]]
    y = [0.5, 2.5]
    clf = tree.DecisionTreeRegressor()
    clf = clf.fit(X, y)
    

    and then, similarly, some code from the classification docs regarding graphviz:

    import graphviz 
    dot_data = tree.export_graphviz(clf, out_file='tree.dot') 
    

    we end up with a file tree.dot, looking like that:

    digraph Tree {
    node [shape=box] ;
    0 [label="X[0] <= 1.0\nmse = 1.0\nsamples = 2\nvalue = 1.5"] ;
    1 [label="mse = 0.0\nsamples = 1\nvalue = 0.5"] ;
    0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
    2 [label="mse = 0.0\nsamples = 1\nvalue = 2.5"] ;
    0 -> 2 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
    }
    

    Now, you can proceed to visualize it as shown in the docs - but if for whatever reason you cannot render Graphviz objects, you can use the handy service WebGraphviz (+1 to the relevant answer in the linked question); the result looks like this:

    Your own answer, i.e. going all the way to install graphlab just for the visualization, sounds like overkill...

    Last remark: don't get deceived by the superficial differences in the tree layouts, which reflect only design choices of the respective visualization packages; the regression tree you have plotted (which, admittedly, does not look much like a tree) is structurally similar to the classification one taken from the docs - simply imagine a top-down tree, with your odor node at the top, followed by your green nodes and terminating to your blue & orange nodes (and replace "yes/no" with "True/False")...

    0 讨论(0)
提交回复
热议问题