赞
踩
学习决策树时,发现如果要用python画决策树,python需要导入Graphviz库,看了几篇csdn,发现个别文章不是很全,会出错误,这里简单记录下完整过程。
dot -version
(一定记得加空格)pip install graphviz
import graphviz
跑一下#导入包和库和数据 import pandas as pd from sklearn import tree from sklearn.datasets import load_wine from sklearn.model_selection import train_test_split wine=load_wine() print(wine.feature_names) print(wine.target_names) #生成DateFrame格式 wine1=pd.concat([pd.DataFrame(wine.data),pd.DataFrame(wine.target)],axis=1) wine1.columns=['alcohol', 'malic_acid', 'ash', 'alcalinity_of_ash', 'magnesium', 'total_phenols', 'flavanoids', 'nonflavanoid_phenols', 'proanthocyanins', 'color_intensity', 'hue', 'od280/od315_of_diluted_wines', 'proline','target'] wine1.head() #划分数据 Xtrain,Xtest,Ytrain,Ytest=train_test_split(wine.data,wine.target,test_size=0.3) #建立模型 clf=tree.DecisionTreeClassifier(criterion="entropy") # entropy基尼系数 #训练数据 clf=clf.fit(Xtrain,Ytrain) #得分 score=clf.score(Xtest,Ytest) score #画决策图 #导入库 import graphviz #特征值重命名 feature_name = ["酒精","苹果酸","灰","灰的碱性","镁","总酚","类黄酮","非黄烷类酚类","花青素","颜色强度","色调","od280/od315稀释葡萄酒","脯氨酸"] dot_data=tree.export_graphviz(clf ,feature_names=feature_name ,class_names=["琴酒","雪莉","贝尔摩德"] ,filled=True ,rounded=True ) graph=graphviz.Source(dot_data) graph
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。