pydotplus无法正常工作,决策树可视化错误?

时间:2019-09-03 11:51:40

标签: python graphviz graph-visualization pydot

实际上,我正在使用sklearn可视化决策树。我已经安装了graphviz和pydotplus,还设置了graphviz的环境变量,但是当我运行此代码时,它给出了一个错误。

我正在使用“驼背”数据集。

这是实际的代码

from IPython.display import Image
from sklearn.externals.six import StringIO
from sklearn.tree import export_graphviz
import pydotplus

features = list(df.columns[1:])
features

Output:
['Age', 'Number', 'Start']

工作正常

但是当我运行下面的代码时。 代码:

dot_data = StringIO()  
export_graphviz(dtree, 
out_file=dot_data,feature_names=features,filled=True,rounded=True)

graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph[0].create_png())  

显示错误。

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call 
last)
<ipython-input-44-28715c87781f> in <module>
      3 
      4 graph = pydotplus.graph_from_dot_data(dot_data.getvalue())
----> 5 Image(graph[0].create_png())

TypeError: 'Dot' object is not subscriptable

1 个答案:

答案 0 :(得分:0)

尝试从if ( !is_array( $results->TStockResponse->item ) ) { $results->TStockResponse->item = [ $results->TStockResponse->item ]; } 中删除[0]

它将正常工作。

您将能够可视化决策树。