How do I find which attributes my tree splits on, when using scikit-learn?

前端 未结 3 1198
余生分开走
余生分开走 2020-12-23 02:35

I have been exploring scikit-learn, making decision trees with both entropy and gini splitting criteria, and exploring the differences.

My question, is how can I \"o

3条回答
  •  星月不相逢
    2020-12-23 03:13

    Directly from the documentation ( http://scikit-learn.org/0.12/modules/tree.html ):

    from io import StringIO
    out = StringIO()
    out = tree.export_graphviz(clf, out_file=out)
    

    StringIO module is no longer supported in Python3, instead import io module.

    There is also the tree_ attribute in your decision tree object, which allows the direct access to the whole structure.

    And you can simply read it

    clf.tree_.children_left #array of left children
    clf.tree_.children_right #array of right children
    clf.tree_.feature #array of nodes splitting feature
    clf.tree_.threshold #array of nodes splitting points
    clf.tree_.value #array of nodes values
    

    for more details look at the source code of export method

    In general you can use the inspect module

    from inspect import getmembers
    print( getmembers( clf.tree_ ) )
    

    to get all the object's elements

    Decision tree visualization from sklearn docs

提交回复
热议问题