首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python -为Graphviz导出最终的随机森林树

Python -为Graphviz导出最终的随机森林树
EN

Stack Overflow用户
提问于 2018-05-16 15:02:08
回答 1查看 855关注 0票数 0

我有一个带有决策树和随机森林的Python代码。决策树使用以下方法查找最大的贡献者:

代码语言:javascript
复制
contr = decisiontree.feature_importances_.max()  * 100
contr_full = decisiontree.feature_importances_  * 100

#Showing name
location = pd.to_numeric(np.where(contr_full == contr)[0][0])
result = list(df_dmy)[location + 1]

这将返回数据集中最大的贡献者,然后使用以下方法将其导出为Graphviz格式:

代码语言:javascript
复制
tree.export_graphviz(rpart, out_file=path_file + '\\Decision Tree Code for Graphviz.dot', filled=True, 
                 feature_names=list(df_dmy.drop(['Reason of Removal'], axis=1).columns), 
                         impurity=False, label=None, proportion=True, 
                         class_names=['Unscheduled', 'Scheduled'], rounded=True)

在随机森林的情况下,我成功地导出了在那里使用的每棵树(100棵树):

代码语言:javascript
复制
i = 0
for tree_data in rf.estimators_:
with open('tree_' + str(i) + '.dot', 'w') as my_file:
    my_file = tree.export_graphviz(tree_data , out_file = my_file)
i = i + 1

当然,这会用不同的树生成100个字文件。然而,并不是每一棵树都包含所需的信息,因为有些树显示的结果不同。我知道分类器最大的贡献者,但我也希望看到决策树的结果。

我试过的是:

代码语言:javascript
复制
i= 0
for tree_data in rf.estimators_:
#Feature importance
df_trees = tree_data.tree_.threshold

contr = df_trees.max()  * 100
contr_full = df_trees * 100

#Showing name
location = pd.to_numeric(np.where(contr_full == contr)[0][0])
result = print(list(df_dmy)[location + 1])

使用这种方法,我得到了错误:IndexError: list索引超出了范围,我不知道这里有什么问题。

我想要一个最大的贡献者的数据和他们的贡献因素,以便过滤到实际最大的贡献者和最大的贡献。见示例:

结果(在数据中)=

代码语言:javascript
复制
    Result   Contribution
0   Car      0.74
1   Bike     0.71
2   Car      0.79

Python已经知道,随机森林的结果将“car”作为最大的贡献者,第一个过滤器是删除除“car”之外的所有内容:

代码语言:javascript
复制
Result   Contribution
0   Car      0.74
2   Car      0.79

然后,它必须搜索最高的贡献和检索索引。

代码语言:javascript
复制
    Result   Contribution
2   Car      0.79

然后,它必须导出与该索引对应的树信息。

我知道这是一个很长的故事,但我希望有人知道如何完成这段代码。

你好,加内什

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-17 18:48:02

代码语言:javascript
复制
names = []
contributors = []

df = pd.DataFrame(columns=['Parameter', 'Value'])

for tree_data in rf.estimators_:
    #Feature importance
    df_trees = tree_data.tree_.threshold

    contr = tree_data.feature_importances_.max()  * 100
    contr_full = tree_data.feature_importances_ * 100

    contr_location = pd.to_numeric(np.where(contr_full == contr)[0][0])
    names.append(list(titanic_dmy.columns)[contr_location + 1])
    contributors.append(contr)

df['Parameter']=np.array(names)
df['Value']=np.array(contributors)
idx = df.index[df['Value'] == df['Value'].loc[df['Value'].idxmax()]].tolist()[0]

#Export to Graphviz
tree.export_graphviz(rf.estimators_[idx], out_file=path_file + '\\RF Decision Tree for Graphviz.dot', 
                     filled=True, max_depth=graphviz_leafs, feature_names=list(titanic_dmy.drop(['survived'], 
                     axis=1).columns), impurity=False, label=None, proportion=True, 
                     class_names=['Unscheduled', 'Scheduled'], rounded=True, precision=2)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50374270

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档