解決使用export_graphviz可視化樹報錯的問題
在使用可視化樹的過程中,報錯了。說是‘dot.exe'not found in path
原代碼:
# import tools needed for visualization
from sklearn.tree import export_graphviz
import pydot
#Pull out one tree from the forest
tree = rf.estimators_[5]
# Export the image to a dot file
export_graphviz(tree, out_file = 'tree.dot', feature_names = features_list, rounded = True, precision = 1)
#Use dot file to create a graph
(graph, ) = pydot.graph_from_dot_file('tree.dot')
# Write graph to a png file
graph.write_png('tree.png');
報錯信息:

解決方法:
先使用安裝pydot:
pip install pydot
然后再下載Graphviz(http://www.graphviz.org 選擇msi版本)一路安裝,記住默認(rèn)的安裝路徑
c:\Program Files (x86)\Graphviz2.38\。
將Graphviz2.38添加到環(huán)境變量中
import os
os.environ['PATH'] = os.environ['PATH'] + (';c:\\Program Files (x86)\\Graphviz2.38\\bin\\')
之后便可以正常使用了。
修改后代碼:
# import tools needed for visualization
from sklearn.tree import export_graphviz
import pydot
import os
os.environ['PATH'] = os.environ['PATH'] + (';c:\\Program Files (x86)\\Graphviz2.38\\bin\\')
#Pull out one tree from the forest
tree = rf.estimators_[5]
# Export the image to a dot file
export_graphviz(tree, out_file = 'tree.dot', feature_names = features_list, rounded = True, precision = 1)
#Use dot file to create a graph
(graph, ) = pydot.graph_from_dot_file('tree.dot')
# Write graph to a png file
graph.write_png('tree.png');
以上這篇解決使用export_graphviz可視化樹報錯的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python網(wǎng)絡(luò)請求之Requests庫的高級功能運用
在這篇文章中我們將進(jìn)一步深入學(xué)習(xí)Requests庫的高級功能,包括處理重定向,設(shè)置超時,處理大文件以及錯誤和異常處理,需要的朋友可以參考下2023-08-08
python?yaml文件數(shù)據(jù)按原有的數(shù)據(jù)順序dump問題小結(jié)
這篇文章主要介紹了python?yaml文件數(shù)據(jù)按原有的數(shù)據(jù)順序dump,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
Tensorflow 2.4 搭建單層和多層 Bi-LSTM 模型
這篇文章主要為大家介紹了Tensorflow 2.4 搭建單層 Bi-LSTM 模型和多層 Bi-LSTM 模型的實現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
tf.truncated_normal與tf.random_normal的詳細(xì)用法
本篇文章主要介紹了tf.truncated_normal與tf.random_normal的詳細(xì)用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

