【问题标题】:'Length of feature names doesn't match with number of features' error while plotting decision tree using Graphviz使用 Graphviz 绘制决策树时出现“特征名称长度与特征数量不匹配”错误
【发布时间】:2018-05-29 08:13:43
【问题描述】:

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

~\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))

你能告诉我出了什么问题吗?

【问题讨论】:

    标签: pandas scikit-learn graphviz


    【解决方案1】:

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

    【讨论】:

    • @pankaj 请你分享代码。 X_train_res 特征名称.. 对我来说,虽然我使用的是 vect.get_feature_name(),但形状变得不匹配.. 仍然存在这个错误。有什么办法可以解决。
    猜你喜欢
    • 2017-01-21
    • 2021-07-25
    • 1970-01-01
    • 2017-11-14
    • 2021-06-02
    • 2020-07-24
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多