我正在尝试使用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))
你能告诉我出了什么问题吗?
答案 0 :(得分:3)
您的data.columns包含所有功能+标签的名称,因为它没有分为X_train_res和y_train_res。您需要传递X_train_res中的功能名称而不是data.columns以获取确切的功能,否则它还将包含标签。我假设在这种情况下X_train和y_train派生自data
。