python - 在 sklearn 中可视化决策树

标签 python tree jupyter-notebook sklearn-pandas

当我想要可视化树时,我遇到了这个错误。

我已经显示了导入的所需库。 jupiter-notebook 有预期的原因吗?

from sklearn import tree
import matplotlib.pyplot
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
cancer=load_breast_cancer()
x=cancer.data
y=cancer.target
clf=DecisionTreeClassifier(max_depth=1000)
x_train,x_test,y_train,y_test=train_test_split(x,y)
clf=clf.fit(x_train,y_train)
tree.plot_tree(clf.fit(x_train,y_train))

AttributeError: module 'sklearn.tree' has no attribute 'plot_tree'

最佳答案

我将树分配给一个对象并添加了 plt.show()。这对我有用。

%matplotlib inline
from sklearn import tree
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()
x = cancer.data
y = cancer.target
clf = DecisionTreeClassifier(max_depth = 1000)
x_train,x_test,y_train,y_test = train_test_split(x,y)

fig = clf.fit(x_train,y_train)
tree.plot_tree(fig)
plt.show()

但我建议使用graphviz,它更加灵活。

关于python - 在 sklearn 中可视化决策树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817742/

相关文章:

python - 在 QTextEdit 中自动添加括号或引号

c++ - 使用 minheap 实现霍夫曼树

python - 如何验证 Jupyter Notebook 中的 python 代码?

python - 在 jupyter 笔记本 python 中加载数据集

algorithm - 砍树,大多数测试用例评估不正确

python - 如何将 Pylint 或其他 linters 与 Jupyter 笔记本一起使用?

python - 放大鼠标位置 QGraphicsView

python - 字典中的函数按值传递

python - 如何使用 pylab 和 numpy 将正弦曲线拟合到我的数据中?

c - 通过前缀在树中查找单词