#四种可视化决策树的方式
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格式