Как извлечь случайные правила лесного дерева для Multiclass Classification? - PullRequest
2 голосов
/ 27 июня 2019

Привет, я хочу извлечь правила из одного дерева в случае классификации нескольких классов

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)


enter image description here

1 Ответ

0 голосов
/ 27 июня 2019

Пожалуйста, проверьте следующий код. Вроде работает. Есть только одно небольшое изменение

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...