使用pydotplus&GraphViz绘制决策树图形

###安装GraphViz之后,需要设置环境变量,还需配套安装pydotplus
https://blog.csdn.net/chai_zheng/article/details/78226556


###葡萄酒公开数据集下载地址:http://archive.ics.uci.edu/ml/datasets/Wine

###随机森林可视化
https://blog.csdn.net/linhai1028/article/details/79827331


import time
import pydotplus
import numpy as np
from sklearn import tree
from sklearn.externals.six import StringIO
from sklearn.model_selection import train_test_split

print('Step 1.Loading data...')
data = np.loadtxt("D:/data/Wine.txt", delimiter=',')
x = data[:, 1:14]
y = data[:, 0].reshape(178,1)
X_train, X_test, Y_train, Y_test = train_test_split(x, y, test_size=0.4)
print('---Loading and splitting completed.')


print('Step 2.Training...')
startTime = time.time()
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train,Y_train)
print('---Training Completed.Took %f s.'%(time.time()-startTime))


print('Step 3.Testing...')
Y_predict = clf.predict(X_test)
matchCount = 0
for i in range(len(Y_predict)):
    if Y_predict[i] == Y_test[i]:
        matchCount += 1
accuracy = float(matchCount/len(Y_predict))
print('---Testing completed.Accuracy: %.3f%%'%(accuracy*100))


feature_name = ['Alcohol','Malic Acid','Ash','Alcalinity of Ash','Magnesium','Total Phenols',
                'Flavanoids','Nonflavanoid Phenols','Proantocyanins','Color Intensity','Hue',
                'OD280/OD315 of Diluted Wines','Proline']
target_name = ['Class1','Class2','Class3']


dot_data = StringIO()

tree.export_graphviz(clf,out_file = dot_data,feature_names=feature_name,
                     class_names=target_name,filled=True,rounded=True,
                     special_characters=True)


graph = pydotplus.graph_from_dot_data(dot_data.getvalue())

graph.write_pdf("WineTree.pdf")
print('Visible tree plot saved as pdf.')


转载于:https://my.oschina.net/kyo4321/blog/3009740

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值