from sklearn import tree tree.export_graphviz(classifier,out_file="tree.dot" ) import pydotplus from IPython.display import Image dot_data=tree.export_graphviz(classifier, out_file=None,feature_names=irisdata.feature_names,filled=True, rounded=True,special_characters=True) graph = pydotplus.graph_from_dot_data(dot_data) 帮我画出决策树
时间: 2024-02-21 07:56:51 浏览: 164
很高兴为您提供帮助!以下是绘制决策树的代码:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import tree
import pydotplus
from IPython.display import Image
# 加载数据集
iris = load_iris()
X = iris.data
y = iris.target
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# 创建决策树模型
classifier = DecisionTreeClassifier(random_state=0)
classifier.fit(X_train, y_train)
# 导出决策树图
dot_data = tree.export_graphviz(classifier, out_file=None,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True, rounded=True,
special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())
```
运行以上代码,即可绘制出决策树图。您可以根据需要将图形保存为PNG或PDF文件。
阅读全文