试图将最优决策树可视化为文本



我试图使用Tree.export_Text((方法将我的决策树可视化为Text,但遇到一个错误,告诉我需要为我的参数网格(param_grid(指定一个参数。如有任何协助,我们将不胜感激。谢谢

param_grid={'max_depth': [3], 'max_leaf_nodes': [4], 'min_samples_split': [2]}
decision_tree = DecisionTreeClassifier(random_state=42, param_grid)
# Retrain the model
decision_tree = decision_tree.fit(X, y)
# Use the tree.export_text() method to visualize the "optimal" decision tree. 
r = export_text(decision_tree, feature_names=iris['feature_names'])
print(r)
error:
File "C:UsersspencAppDataLocalTemp/ipykernel_15384/3344812307.py", line 8
decision_tree = DecisionTreeClassifier(random_state=42, param_grid)
^
SyntaxError: positional argument follows keyword argument

尝试更正以下内容:

  1. 首先实例化一个新的DecisionTreeClassifier对象;最佳参数";你之前已经开始搜索了。

  2. 在重新训练最优模型时,将(X,y(替换为(X_train,y_train(。

  3. 同时将树添加到您的export_text((中,作为tree.export_text(。

我希望这能有所帮助,或者至少能让您更接近解决方案。:(

最新更新