pip install graphviz Collecting graphvizNote: you may need to restart the kernel to use updated packages. Downloading graphviz-0.20.1-py3-none-any.whl (47 kB) Installing collected packages: graphviz Successfully installed graphviz-0.20.1 import graphviz dot_data=tree.export_graphviz(clf,feature_names= feature_name ,class_names=["琴酒","雪莉","贝尔摩德"] ,filled=True ,rounded=True) graph = graphviz.Source(dot_data) graph --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [14], in <cell line: 2>() 1 import graphviz ----> 2 dot_data=tree.export_graphviz(clf,feature_names= feature_name 3 ,class_names=["琴酒","雪莉","贝尔摩德"] 4 ,filled=True 5 ,rounded=True) 7 graph = graphviz.Source(dot_data) 8 graph AttributeError: 'DecisionTreeClassifier' object has no attribute 'export_graphviz'
时间: 2023-10-07 09:13:24 浏览: 167
graphviz安装包(我是用python画’决策树‘图的时候用的)
这个错误提示意思是 `DecisionTreeClassifier` 类中没有名为 `export_graphviz` 的方法或属性,因此在调用该方法时出错。
正确的方法名应该是 `export_graphviz`,它可以将决策树模型导出为 Graphviz 格式的图形化表示。
你需要将 `export_graghviz` 改为 `export_graphviz`,并确保你导入了正确的 `tree` 模块。你可以尝试使用以下代码:
```
from sklearn.tree import export_graphviz
import graphviz
dot_data = export_graphviz(clf, feature_names=feature_name, class_names=["琴酒","雪莉","贝尔摩德"], filled=True, rounded=True)
graph = graphviz.Source(dot_data)
graph
```
这应该可以成功绘制决策树图。
阅读全文