Python - 为 Graphviz 导出最终的随机森林树
Python - export the final random forests tree for Graphviz
我有一个带有决策树和随机森林的 Python 代码。决策树使用以下方法找到最大的贡献者:
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]
这 returns 是我数据集中最大的贡献者,然后使用以下方法导出为 Graphviz 格式:
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 棵树):
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 个包含不同树的单词文件。然而,并非每棵树都包含所需的信息,因为有些树会显示不同的结果。我确实知道分类器的最大贡献者,但我也想查看具有该结果的决策树。
我试过的是:
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: 列表索引超出范围
我不知道这里出了什么问题。
我想要一个包含最大贡献者及其贡献因素的数据框,以便将其过滤为实际的最大贡献者和最大贡献。参见示例:
结果(在数据框中)=
Result Contribution
0 Car 0.74
1 Bike 0.71
2 Car 0.79
Python 已经知道随机森林的结果给 'car' 作为最大的贡献者,第一个过滤器是删除除 'car':
之外的所有内容
Result Contribution
0 Car 0.74
2 Car 0.79
然后它必须搜索最高贡献并检索索引。
Result Contribution
2 Car 0.79
然后要导出那个索引对应的树信息。
我知道这是一个很长的故事,但我希望有人知道如何完成这段代码。
问候,加内什
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)
我有一个带有决策树和随机森林的 Python 代码。决策树使用以下方法找到最大的贡献者:
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]
这 returns 是我数据集中最大的贡献者,然后使用以下方法导出为 Graphviz 格式:
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 棵树):
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 个包含不同树的单词文件。然而,并非每棵树都包含所需的信息,因为有些树会显示不同的结果。我确实知道分类器的最大贡献者,但我也想查看具有该结果的决策树。
我试过的是:
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: 列表索引超出范围 我不知道这里出了什么问题。
我想要一个包含最大贡献者及其贡献因素的数据框,以便将其过滤为实际的最大贡献者和最大贡献。参见示例:
结果(在数据框中)=
Result Contribution
0 Car 0.74
1 Bike 0.71
2 Car 0.79
Python 已经知道随机森林的结果给 'car' 作为最大的贡献者,第一个过滤器是删除除 'car':
之外的所有内容Result Contribution
0 Car 0.74
2 Car 0.79
然后它必须搜索最高贡献并检索索引。
Result Contribution
2 Car 0.79
然后要导出那个索引对应的树信息。
我知道这是一个很长的故事,但我希望有人知道如何完成这段代码。
问候,加内什
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)