Sklearn——决策树可视化

#四种可视化决策树的方式
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas.io import feather_format
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn import tree

#加载数据集
data = load_iris()
#转换成DataFrame形式
df = pd.DataFrame(data.data, columns= data.feature_names)
#添加品种列
df['Species'] = data.target

#用数值替代品种名作为标签
target = np.unique(data.target)
target_names = np.unique(data.target_names)
targets = dict(zip(target, target_names))
df['Species'] = df['Species'].replace(targets)

#提取数据和标签
X = df.drop(columns="Species")
y = df["Species"]
feature_names = X.columns
labels = y.unique()

X_train, test_x, y_train, test_lab = train_test_split(X, y, test_size = 0.4, random_state = 42)
model = DecisionTreeClassifier(max_depth =3, random_state = 42)
model.fit(X_train, y_train)

方法一

直接使用sklearn.tree中的export_text方法,以文字形式输出树

#文字表示
#以文字形式输出树
text_representation = tree.export_text(model)
print(text_representation)

在这里插入图片描述

方法二

直接使用sklearn.tree自带的plot_tree()方法

#plot_tree函数
#用图片画出
plt.figure(figsize(30,10), facecolor= 'g')
a = tree.plot_tree(model,
                    feature_names = feature_names,
                    class_names= labels,
                    rounded=True,
                    filled= True,
                    fontsize=14)
#plt.show()
plt.savefig('tree.png')

在这里插入图片描述

方法三

使用Graphviz,需要安装graphviz模块, pip install graphviz

#DOT data
import graphviz

dot_data = tree.export_graphviz(model, 
                                out_file=None,
                                feature_names=data.feature_names,
                                class_names=data.target_names,
                                filled= True)
#Draw graph
graph = graphviz.Source(dot_data, format="png")
graph.render('tree')

方法四

使用pydotplus模块

pydotplus依赖Graphviz这个绘图库, 它是c开发的, 所以在安装pydotplus之pip install pydotplus

import pydotplus
graph = pydotplus.graph_from_dot_data(dot_data)
graph.write_pdf('example.pdf')    #保存图像为pdf格式

在这里插入图片描述

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值