pydotplus的安装、基本入门和决策树的可视化( 二 )

4.2.3 数据集:打开网页 , 复制 , 并修改txt为csv
4.3 代码:
import numpy as npimport pandas as pdfile_path = "/home/xgj/Desktop/pydotplus/titanic.csv"data = http://kandian.youth.cn/index/pd.read_csv(file_path)data["age"].isnull().sum()# 年龄有680个缺失值x = data[["pclass", "age", "sex"]]y = data["survived"]x["age"].fillna(x["age"].mean(), inplace=True)# 用平均年龄来填充缺失值from sklearn.model_selection import train_test_split# 导入数据集分割from sklearn.feature_extraction import DictVectorizer# 导入特征工程from sklearn.tree import DecisionTreeClassifier# 导入决策树分类器x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.25)# 分割数据info = DictVectorizer(sparse=False)# 特征工程x_train = info.fit_transform(x_train.to_dict(orient="records"))x_test = info.fit_transform(x_test.to_dict(orient="records"))dec = DecisionTreeClassifier(max_depth=5)dec.fit(x_train, y_train)dec.score(x_test, y_test)dec.predict(x_test[0: 1])# 进行测试预测from sklearn import treeimport pydotplus# 可视化dot_data = http://kandian.youth.cn/index/tree.export_graphviz(dec, out_file=None,filled=True, rounded=True,special_characters=True)graph = pydotplus.graph_from_dot_data(dot_data)graph.get_nodes()[7].set_fillcolor("#FFF2DD")graph.write_png("/home/xgj/Desktop/pydotplus/graph7.png")5 加载sklearn自身数据集
===================
5.1 效果图
pydotplus的安装、基本入门和决策树的可视化文章插图
5.2 参考文章
#;depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-4.add_param_isCf5.3 代码
from sklearn import treefrom sklearn.datasets import load_winefrom sklearn.model_selection import train_test_splitwine=load_wine()#集合自身的数据集Xtrain,Xtest,Ytrain,Ytest=train_test_split(wine.data,wine.target,test_size=0.3)#训练clf=tree.DecisionTreeClassifier(criterion='entropy')clf=clf.fit(Xtrain,Ytrain)#查看一下准确度score=clf.score(Xtest,Ytest)f_name=['酒精','苹果酸','灰','灰的碱性','镁','总酚','类黄酮','非黄烷类酚类','花青素','颜色强度','色调','稀释葡萄酒','脯氨酸']dot_data=http://kandian.youth.cn/index/tree.export_graphviz(clf,feature_names=f_name,class_names=['茅台','啤酒','黄酒'],filled=True,rounded=True,out_file=None)import pydotplus# 可视化graph = pydotplus.graph_from_dot_data(dot_data)graph.write_png("/home/xgj/Desktop/pydotplus/graph8.png")6 加载自身数据集、生成dot文件和决策树
===============================
6.1 图
pydotplus的安装、基本入门和决策树的可视化文章插图
6.2 代码:
#参考文章#from sklearn import treefrom sklearn.datasets import load_iris#导入数据集iris = load_iris()clf = tree.DecisionTreeClassifier()clf = clf.fit(iris.data, iris.target)#生成dot文件with open("/home/xgj/Desktop/pydotplus/iris.dot", 'w') as f:f = tree.export_graphviz(clf, out_file=f)#决策树pydotplus可视化import pydotplus dot_data = http://kandian.youth.cn/index/tree.export_graphviz(clf, out_file=None) graph = pydotplus.graph_from_dot_data(dot_data) graph.write_png("/home/xgj/Desktop/pydotplus/iris.png")