使用Graphviz绘制决策树时出现“要素名称的长度与要素数量不匹配"错误 [英] 'Length of feature names doesn't match with number of features' error while plotting decision tree using Graphviz

查看:240
本文介绍了使用Graphviz绘制决策树时出现“要素名称的长度与要素数量不匹配"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用graphviz可视化决策树,并在尝试绘制决策树时出现以下错误:

I am trying to visualize a decision tree using graphviz and while trying to plot the decision tree I am getting the below error :

~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\tree\export.py in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters, precision)
    427                                  "does not match number of features, %d"
    428                                  % (len(feature_names),
--> 429                                     decision_tree.n_features_))
    430 
    431         # The depth of each node for plotting with 'leaf' option

ValueError: Length of feature_names, 225 does not match number of features, 

208

我的代码

dt=DecisionTreeClassifier(class_weight="balanced", min_samples_leaf=30)
fit_decision=dt.fit(X_train_res,y_train_res)
from graphviz import Source
from sklearn import tree
Source( tree.export_graphviz(fit_decision, out_file=None, feature_names=data.columns))

你能告诉我哪里出了问题吗?

Can you tell me what went wrong?

推荐答案

您的data.columns包含所有features + label的名称,因为它没有分为X_train_res和y_train_res.您需要在X_train_res中传递要素名称,而不是data.columns来获得确切的要素,否则它将包括标签.在这种情况下,我假设X_train和y_train是从 data 派生的.

Your data.columns contains names of all the features+label since it is not divided into X_train_res and y_train_res. You need to pass the feature names in in X_train_res instead of data.columns to get the exact features else it will include the labels as well. I am assuming that X_train and y_train are derived from data in this case.

这篇关于使用Graphviz绘制决策树时出现“要素名称的长度与要素数量不匹配"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆