Scikit learn 向决策树添加正确的标签

Scikit learn 向决策树添加正确的标签,scikit-learn,decision-tree,Scikit Learn,Decision Tree,我正在机器学习项目中使用随机森林回归器。为了更好地理解预测的逻辑,我想可视化一些决策树,并检查在预测时使用了哪些功能 为此,我编写了以下代码: from sklearn.tree import export_graphviz from subprocess import call from IPython.display import Image # Select one estimator from the Random Forests estimator = best_estimators

我正在机器学习项目中使用随机森林回归器。为了更好地理解预测的逻辑,我想可视化一些决策树,并检查在预测时使用了哪些功能

为此,我编写了以下代码:

from sklearn.tree import export_graphviz
from subprocess import call
from IPython.display import Image

# Select one estimator from the Random Forests
estimator = best_estimators_regr['RandomForestRegressor'][0].estimators_[0]

export_graphviz(estimator, out_file=path+'tree.dot', 
           rounded=True, proportion=False, 
           precision=2, filled=True)
call(['dot', '-Tpng', path+'tree.dot', '-o', path+'tree.png', '-Gdpi=600'])
Image(filename=path+'tree.png')

问题是我在训练模型时使用了
max\u features
参数,因此我不知道每个树中使用了哪些特征。因此,当绘制一棵树时,我只需得到
X[一些数字]
。该数字是否与原始数据集中的列相对应?如果没有,我如何告诉它使用列的名称而不是数字?

使用
RandomForestClassifier
中的
'max\u features'
参数一次获取特征的数量,以找到最佳分割。该参数被传递给所有单个估计器(
DecisionTreeClassifier
)。基本
DecisionTreeClassifier
对象都接受整个数据(样本从训练数据中采样,但所有列特征都传递给每个树)。特征顺序决定为单个
DecisionTreeClassifier
对象。所以不用担心


您可以使用
export\u graphviz
中的
feature\u names
参数传递所有功能的每个功能的名称。

如果我理解正确,我只需将
columns
参数从我的数据框传递到
feature\u names
,对吗?谢谢