如何可视化sklearn GradientBoostingClassifier?

时间:2017-07-07 15:18:34

标签: scikit-learn graphviz decision-tree

我已经培训了gradient boost classifier,我想使用here显示的graphviz_exporter工具对其进行可视化。

当我尝试时,我得到:

AttributeError: 'GradientBoostingClassifier' object has no attribute 'tree_'

这是因为graphviz_exporter适用于decision trees,但我想还有一种可视化方法,因为渐变提升分类器必须有一个基础决策树。

有人知道怎么做吗?

1 个答案:

答案 0 :(得分:8)

属性估算器包含基础决策树。以下代码显示受过训练的GradientBoostingClassifier的树之一。

clf = GradientBoostingClassifier(
    n_estimators=200,
    learning_rate=1.0,
    max_depth=3,
    random_state=42
)
clf = clf.fit(X[:600], Y[:600])


# Get the tree number 42
sub_tree_42 = clf.estimators_[42, 0]

dot_data = tree.export_graphviz(
    sub_tree_42,
    out_file=None, filled=True,
    rounded=True,  
    special_characters=True,
    proportion=True,
)
graph = pydotplus.graph_from_dot_data(dot_data)  
Image(graph.create_png())