这是我在您的环境中运行它的代码,我正在使用 RandomForestClassifier 并且我试图找出 decision_path 中选定示例的 RandomForestClassifier

import numpy as np
import pandas as pd
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier

X, y = make_classification(n_samples=1000,
                           n_features=6,
                           n_informative=3,
                           n_classes=2,
                           random_state=0,
                           shuffle=False)

# Creating a dataFrame
df = pd.DataFrame({'Feature 1':X[:,0],
                                  'Feature 2':X[:,1],
                                  'Feature 3':X[:,2],
                                  'Feature 4':X[:,3],
                                  'Feature 5':X[:,4],
                                  'Feature 6':X[:,5],
                                  'Class':y})


y_train = df['Class']
X_train = df.drop('Class',axis = 1)

rf = RandomForestClassifier(n_estimators=50,
                               random_state=0)

rf.fit(X_train, y_train)
我得到的最远的是这样的:
#Extracting the decision path for instance i = 12
i_data = X_train.iloc[12].values.reshape(1,-1)
d_path = rf.decision_path(i_data)

print(d_path)
但输出没有多大意义:

我试图找出数据帧中粒子样本的决策路径。谁能告诉我怎么做?
这个想法是有类似 this 的东西。

最佳答案

RandomForestClassifier.decision_path 方法返回 tuple(indicator, n_nodes_ptr)
请参阅文档:
here

所以你的变量 node_indicator 是一个元组,而不是你认为的那样。
元组对象没有属性“索引”,这就是为什么您在执行以下操作时会收到错误:

node_index = node_indicator.indices[node_indicator.indptr[sample_id]:
                                    node_indicator.indptr[sample_id + 1]]

尝试 :
(node_indicator, _) = rf.decision_path(X_train)

您还可以为单个样本 id 绘制森林中每棵树的决策树:
X_train = X_train.values

sample_id = 0

for j, tree in enumerate(rf.estimators_):

    n_nodes = tree.tree_.node_count
    children_left = tree.tree_.children_left
    children_right = tree.tree_.children_right
    feature = tree.tree_.feature
    threshold = tree.tree_.threshold

    print("Decision path for DecisionTree {0}".format(j))
    node_indicator = tree.decision_path(X_train)
    leave_id = tree.apply(X_train)
    node_index = node_indicator.indices[node_indicator.indptr[sample_id]:
                                        node_indicator.indptr[sample_id + 1]]



    print('Rules used to predict sample %s: ' % sample_id)
    for node_id in node_index:
        if leave_id[sample_id] != node_id:
            continue

        if (X_train[sample_id, feature[node_id]] <= threshold[node_id]):
            threshold_sign = "<="
        else:
            threshold_sign = ">"

        print("decision id node %s : (X_train[%s, %s] (= %s) %s %s)"
              % (node_id,
                 sample_id,
                 feature[node_id],
                 X_train[sample_id, feature[node_id]],
                 threshold_sign,
                 threshold[node_id]))

请注意,在您的情况下,您有 50 个估算器,因此阅读起来可能有点无聊。

关于python - 随机森林分类器的决策路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48869343/

10-12 03:26