码迷,mamicode.com
首页 > 其他好文 > 详细

决策树模型

时间:2020-02-10 13:52:59      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:state   file   name   sel   and   span   exp   code   selection   

1 from sklearn import tree,datasets
2 from sklearn.model_selection import train_test_split
3 wine=datasets.load_wine()
4 X,y=wine.data[:,:2],wine.target
5 X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=1)
6 
7 clf=tree.DecisionTreeClassifier(max_depth=3)
8 clf.fit(X_train,y_train)
9 print("the score of this model:{}".format(clf.score(X_test,y_test)))
 1 from matplotlib.colors import ListedColormap
 2 cmap_light=ListedColormap([#FFAAAA,#AAFFAA,#AAAAFF])
 3 cmap_bold=ListedColormap([#FF0000,#00FF00,#0000FF])
 4 
 5 import numpy as np
 6 from matplotlib import pyplot as plt
 7 x_min=X_train[:,0].min()-1
 8 x_max=X_train[:,0].max()+1
 9 y_min=X_train[:,1].min()-1
10 y_max=X_train[:,1].max()+1
11 xx,yy=np.meshgrid(np.arange(x_min,x_max,.02),
12                   np.arange(y_min,y_max,.02))
13 z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
14 z=z.reshape(xx.shape)
15 plt.figure()
16 plt.pcolormesh(xx,yy,z,cmap=cmap_light)
17 #plt.pcolormesh(xx,yy,z,cmap=plt.cm.Pastel1)
18 plt.scatter(X[:,0],X[:,1],c=y,cmap=cmap_bold,edgecolors=k,s=20)
19 plt.xlim(xx.min(),xx.max())
20 plt.ylim(yy.min(),yy.max())
21 plt.title("Decision Tree(max depth=3)")
22 plt.show()
1 import graphviz
2 from sklearn.tree import export_graphviz
3 export_graphviz(clf,out_file="wine.dot",class_names=wine.target_names,
4                 feature_names=wine.feature_names[:2],impurity=False,filled=True)
5 with open("wine.dot") as f:
6     dot_graph = f.read()
7 dot=graphviz.Source(dot_graph)
8 dot.view()

 

决策树模型

标签:state   file   name   sel   and   span   exp   code   selection   

原文地址:https://www.cnblogs.com/St-Lovaer/p/12290502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!