可视化决策树模型
程序员文章站
2022-05-26 19:01:33
...
可视化决策树模型
创建dot数据
pdf 生成png图片
# 可视化决策树模型
import pydotplus
from sklearn.tree import DecisionTreeClassifier
from sklearn import datasets
from IPython.display import Image
from sklearn import tree
iris = datasets.load_iris()
features = iris.data
target = iris.target
decisiontree = DecisionTreeClassifier(random_state=0)
model = decisiontree.fit(features, target)
# 创建dot数据
dot_data = tree.export_graphviz(decisiontree, out_file=None, feature_names=iris.feature_names, class_names=iris.target_names)
# 画图
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())
# create pdf
#graph.write_pdf("iris.pdf")
# create png PNG 生成png图片
#graph.write_png("iris.png)