如何在决策树的节点上获取变量?我得到关键错误 7

问题描述 投票:0回答:1

我即将根据我的模型可视化因果决策树。我终于成功地绘制了树,但不知何故它不显示我的变量名称,而是在节点名称上显示 X[5]。一旦我添加“feature_names=X”,它就不再绘制,而是显示 **“Key Error 7” ** 有人可以帮忙吗? 非常感谢 酶联免疫吸附试验

import numpy as np
import pandas as pd
import graphviz
from econml.dml import CausalForestDML
from econml.cate_interpreter import SingleTreeCateInterpreter


#load cdv
data = pd.read_csv("Basis_Entscheidungsbaum.csv", sep=";", header=0)

#Variables 
feature_names=['DL', 'KE', 'AA', 'K', 'ST', 'G', 'BV', 'A']

Y = data['Z']
T = data['M']
X = data[feature_names]

#tree model
tree_model = CausalForestDML(n_estimators=1, subforest_size=1, inference=False, max_depth=4)

#causal decision tree
tree_model = tree_model.fit(Y=Y, X=X , T=T)
intrp = SingleTreeCateInterpreter(max_depth=3).interpret(tree_model, X)

#Visualization
intrp.plot(fontsize=12)

# intrp.plot(feature_names=X, fontsize=12)

我期望节点上的变量名称

**Input:**
| A | BV | G    | ST | K |  AA  | KE | DL | Z | M | 
|:-:|:--:|:----:|:--:|:-:|:----:|:--:|:--:|:-:|:-:|
|32 | 14 | 3400 | 1  | 0 | 7,49 | 2  | 3  | 0 | 1 | 
python visualization decision-tree
1个回答
0
投票

According to

SingleTreeCateInterpreter
's documentation,如果你想要功能名称,方法
plot
需要一个字符串列表:

plot(ax=None,title=None,feature_names=None,treatment_names=None,max_depth=None,filled=True,rounded=True,precision=3,fontsize=None)

  • feature_names(str 列表,可选)——每个功能的名称。

由于您之前已经生成了此列表,因此您可以直接传递它:

intrp.plot(feature_names=feature_names, fontsize=12)
© www.soinside.com 2019 - 2024. All rights reserved.