如何在Python中可视化回归树 [英] How to visualize a Regression Tree in Python

查看:762
本文介绍了如何在Python中可视化回归树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望可视化使用scikit learning中的任何集成方法(梯度增强回归,随机森林回归,袋装回归)构建的回归树。



您自己的答案,即一味地安装 graphlab 仅用于可视化,听起来像是过分杀人……



最后一句话:不要被树形布局的肤浅差异所欺骗,后者仅反映各个可视化程序包的设计选择;您绘制的回归树(顺便说一句,它看起来不太像 tree )在结构上与从文档中提取的分类相似-只需想象一个自顶向下的树,其中您的<$顶部的c $ c> odor 节点,然后是绿色节点,并终止于蓝色&橙色节点(并将是/否替换为真/假)...


I'm looking to visualize a regression tree built using any of the ensemble methods in scikit learn (gradientboosting regressor, random forest regressor,bagging regressor). I've looked at this question which comes close, and this question which deals with classifier trees. But these questions require the 'tree' method, which is not available to the regression models in SKLearn.

but it didn't seem to yield a result. I'm running into issues because there is no .tree method for the regression versions of these trees (the method only exists for the classification versions). I'd like an output resembling this but based on a sci kit learn-constructed tree.

I've explored the methods associated with the objects but just cannot produce an answer.

解决方案

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")...

这篇关于如何在Python中可视化回归树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆