调用sklearn.tree.plot_tree函数绘制决策树
时间: 2023-05-31 17:05:24 浏览: 146
sklearn中的决策树
以下是使用sklearn.tree.plot_tree函数绘制决策树的示例代码:
```python
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import plot_tree
# 加载鸢尾花数据集
iris = load_iris()
X = iris.data
y = iris.target
# 创建决策树模型
clf = DecisionTreeClassifier(random_state=0)
clf.fit(X, y)
# 绘制决策树
plot_tree(clf)
```
运行该代码将会绘制出如下所示的决策树:
```
X[3] <= 0.8
/ \
(class: 0) X[2] <= 4.75
/ \
(class: 1) (class: 2)
```
阅读全文