【问题标题】:How to extract random forest tree rules for a Multiclass Classification?如何为多类分类提取随机森林树规则?
【发布时间】:2019-11-09 11:50:47
【问题描述】:

你好我想在多类分类的情况下从一棵树中提取规则

from sklearn.tree import _tree 
from sklearn.tree import DecisionTreeClassifier

#creat a gaussian classifier
clf=RandomForestClassifier(n_estimators=100)

#train the model using the training sets y_pred=clf.predict(X_test)

clf.fit(X_train,y_train)

#extract one tree from the forest
model = clf.estimators_[0]


def find_rules(tree,features): 
    dt=tree.tree_
    def visitor(node,depth):
        indent= ' ' * depth
        if dt.feature[node] != _tree.TREE_UNDEFINED:
            print('{} if <{}> <= {}:'.format(indent,features[node],round(dt.threshold[node],100)))
            visitor(dt.children_left[node],depth+1)
            print('{}else:'.format(indent))
            visitor(dt.children_right[node],depth+1)
        else:
            print('{} return {}'.format(indent,dt.value[node]))
    visitor(0,1)


find_rules(model, iris.feature_names)


【问题讨论】:

    标签: python machine-learning scikit-learn random-forest


    【解决方案1】:

    请检查以下代码。它似乎工作。只有一个小变化

    def find_rules(tree,features): 
        dt=tree.tree_
        def visitor(node,depth):
            indent= ' ' * depth
            if dt.feature[node] != _tree.TREE_UNDEFINED:
                print('{} if <{}> <= {}:'.format(indent,features[dt.feature[node]],round(dt.threshold[node],100)))
                # in the previous line i added a backward-mapping
                # for the feature id
                visitor(dt.children_left[node],depth+1)
                print('{} else:'.format(indent))
                visitor(dt.children_right[node],depth+1)
            else:
                print('{} return {}'.format(indent,dt.value[node]))
        visitor(0,1)
    

    【讨论】:

      猜你喜欢
      • 2020-08-16
      • 2013-12-31
      • 2018-07-10
      • 2017-03-15
      • 2019-09-05
      • 2013-09-22
      • 2018-02-18
      • 2020-01-31
      • 2018-11-09
      相关资源
      最近更新 更多