我已经在泰坦尼克数据集上实现了一个标准的随机森林分类器,并希望探索sklearn的decision_path方法,它是在v0.18中引入的。(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)输出:
(<1x3982型稀疏矩阵,具有598个存储元素的压缩稀疏行format>,数组( 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)
发布于 2017-03-14 18:59:04
如果您想要可视化森林中的树木,您可以尝试这里提供的答案: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这将创建10个(森林中树的默认数目)文件,名为tree_i.dot for i=0至9。
$ dot -Tpdf tree_0.dot -o tree.pdf也许有一种更聪明的方法,如果有人能帮忙的话,我很乐意学习它:)
https://stackoverflow.com/questions/42793341
复制相似问题