sklearn 决策树可视化
程序员文章站
2022-04-08 12:59:49
...
来自google developer 的Machine Learning Recipes with Josh Gordon
Youtube链接
https://www.youtube.com/watch?v=cKxRvEZd3Mw&list=PLOU2XLYxmsIIuiBfYad6rFYQU_jL2ryal&index=1
生成决策树
这里使用了lris_flower_data_set
生成了一个认花的小树
import numpy as np
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
test_idx=[0,50,100]
#train data
train_target =np.delete(iris.target,test_idx)
train_data = np.delete(iris.data,test_idx,axis = 0)
#testing fata
test_target = iris.target[test_idx]
test_data = iris.data[test_idx]
clf = tree.DecisionTreeClassifier()
clf.fit(train_data,train_target)
print(clf.predict(test_data))
这里总共有3类鸢尾花,每类50条数据,每组的第一个数据作为测试数据,剩余所有的数据作为训练数据
输出:
[0 1 2]
可视化
使用pydot
可以pip安装
from sklearn.externals.six import StringIO
import pydot
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data,
feature_names=iris.feature_names,
class_names=iris.target_names,
filled=True, rounded=True,
special_characters=True)
graph = pydot.graph_from_dot_data(dot_data.getvalue())
graph[0].write_pdf("iris.pdf")
iris.pdf:
Tips of 安装graphviz
首先需要pip安装graphviz包
pip install graghviz
但是仅仅安装python包会产生
RuntimeError: failed to execute ['dot', '-Tpdf', '-O', 'test-output/round-table.gv'], make sure the Graphviz executables are on your systems' path
解决方案:安装了anaconda可以
conda install graphviz
然后将anaconda\Library\bin\graphviz加入Path可以使用
上一篇: 可视化决策树
下一篇: 斐波那契数列穷竭算法优化