【问题标题】:Random Forest Classifier decision path method (scikit)随机森林分类器决策路径方法(scikit)
【发布时间】:2017-08-05 05:20:47
【问题描述】:

我已经在 Titanic 数据集上实现了一个标准的随机森林分类器,并希望探索 sklearn 在 v0.18 中引入的 decision_path 方法。 (http://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html)

但是,它会输出一个稀疏矩阵,我不确定如何理解。谁能建议如何最好地可视化这一点?

#Training a simplified random forest
estimator = RandomForestClassifier(random_state=0, n_estimators=3, max_depth=3)
estimator.fit(X_train, y_train)

#Extracting the decision path for instance i = 12
i_data = X_test.iloc[12].values.reshape(1,-1)
d_path = rf_best.decision_path(i_data)

print(d_path)

输出:

(, array([ 0, 45,
98、149、190、233、258、309、360、401、430、 461、512、541、580、623、668、711、760、803、852、889、 932、981、1006、1035、1074、1107、1136、1165、1196、1241、1262、 1313、1350、1385、1420、1465、1518、1553、1590、1625、1672、1707、 1744、1787、1812、1863、1904、1945、1982、2017、2054、2097、2142、 2191、2228、2267、2304、2343、2390、2419、2456、2489、2534、2583、 2632、2677、2714、2739、2786、2833、2886、2919、2960、2995、3032、 3073、3126、3157、3194、3239、3274、3313、3354、3409、3458、3483、 3516、3539、3590、3629、3660、3707、3750、3777、3822、3861、3898、 3939, 3982], dtype=int32))

如果我没有提供足够的详细信息,请致歉 - 否则请告诉我。

谢谢!

注意:经过编辑以简化随机森林(限制深度和 n_trees)

【问题讨论】:

  • 您可能希望从一个很小的样本数据集开始,并仅使用森林中的几棵树以使结果更简单。
  • 我也很想知道这一点。从文档来看,如何解释输出并不是很清楚。我唯一知道的是,Sklearn 有一个关于如何可视化决策表面的教程:scikit-learn.org/stable/auto_examples/ensemble/…
  • @BrenBarn 我减少了树的数量以使结果更简单,并且只使用了单个数据实例。希望这会有所帮助。

标签: python scikit-learn random-forest


【解决方案1】:

如果您想可视化森林中的树木,可以尝试此处提供的答案:https://stats.stackexchange.com/q/118016

适应你的问题:

from sklearn import tree

...

i_tree = 0
for tree_in_forest in estimator.estimators_:
    with open('tree_' + str(i_tree) + '.dot', 'w') as my_file:
        my_file = tree.export_graphviz(tree_in_forest, out_file = my_file)
    i_tree = i_tree + 1

这将为 i = 0 到 9 创建 10 个(森林中的默认树数)名为 tree_i.dot 的文件。您可以为在终端上执行的每个文件创建 pdf 文件(例如):

$ dot -Tpdf tree_0.dot -o tree.pdf

可能有更聪明的方法可以做到这一点,如果有人可以提供帮助,我很乐意学习:)

【讨论】:

  • 谢谢卢西亚娜。虽然这无疑是有用的,但我们仍然遇到了可视化整个森林(而不是单个树)的决策路径的问题。
  • @jc023.我也有同样的疑问。你找到解决方案了吗?你能帮我解释一下结果吗
  • @Jayashree TreeInterpreter 包也适用于森林。
  • @ldavid ,TreeIntrepeter 描述的是特征贡献而不是算法的决策路径
猜你喜欢
  • 2018-07-29
  • 2017-03-15
  • 2016-03-24
  • 2018-07-30
  • 2018-12-01
  • 2021-02-26
  • 2015-09-28
  • 2016-12-24
  • 2017-08-25
相关资源
最近更新 更多