#-*-coding:utf-8-*-import chch.set_ch () import matplotlib.pyplot as Pltdecisionnode = dict (Boxstyle = "Sawtooth", fc= " 0.8 ") Leafnode = Dict (boxstyle=" Round4 ", FC =" 0.8 ") Arrow_args = dict (Arrowstyle =" <-") #建立标注annotatedef Plotnode ( Nodetxt,centerpt,parentpt,nodetype): # Callout Content Label position label position C ReatePlot.ax1.annotate (nodetxt,xy=parentpt,xycoords = ' axes fraction ', Xytext = centerpt,textcoords= ' axes fraction ', Va= "Center", #标签的格式 the format of the arrow ha = "center", bbox = Nodetype,arrowprops=dict (arrowstyle= "<-")) def createplottemp (): #图名, can be a digital background color fig = Plt.figure ("Xihuan", Facecolor = ' white ') FIG.CLF () #clear the figure createplottemp.ax1 = Plt.subplot (111,frameon = False) #产生一个子图, do not display axes, but have coordinates plotnode (U ' decision node ', (0.5,0.1), (0.1,0.5), Decisionnode) plotnode (U ' leaf node ', (0.8,0.1), (0.3,0.8 ), Leafnode) plt.show () #计算决策树的叶子节点的数目def geTnumleafs (mytree): Numleafs = 0 firststr = Mytree.keys () [0] seconddict = mytree[firststr] for key in Seconddic T.keys (): If Type (Seconddict[key]) ==dict:numleafs + = Getnumleafs (Seconddict[key]) Else:numleafs + = 1 return numleafs# depth of the compute Tree def gettreedepth (mytree): maxDepth = 0 firststr = Mytree.keys () [0] seconddict = my TREE[FIRSTSTR] for key in Seconddict.keys (): If Type (Seconddict[key]) ==dict:thisdepth = 1 + gettreed Epth (Seconddict[key]) else:thisdepth = 1; If thisdepth > maxdepth:maxdepth = thisdepth return maxdepth# generate a decision tree Def retrievetree (i): Listoftrees = [{' No surf Acing ': {0: ' No ', 1:{' flippers ': {0: ' No ', 1: ' Yes '}}}, {' No surfacing ': {0: ' No ', 1:{' fl Ippers ': {0:{' head ': {0: ' No ', 1: ' Yes '}}, 1: ' No '}}}] return Listoftrees[i]def pl Otmidtext (centrpt,parentpt,txtstring): Xmid = (parentpt[0]-centrpt[0])/2.0+centrpt[0] Ymid = (parentpt[1]-centrpt[1])/2.0+centrpt[1] CreatePlot.ax1.text (xmid,ymid,txtstring) def createplot (intree): FI g = plt.figure ("Xihuan", Facecolor = ' white ') fig.clf () Axprops = Dict (xticks=[],yticks=[]) Createplot.ax1 = Plt.s Ubplot (111,frameon = false,**axprops) Plottree.totalw = float (Getnumleafs (intree)) Plottree.totald = float (getTreeDe PTH (intree)) Plottree.xoff = -0.5/plottree.totalw;plottree.yoff = 1.0; #为了保证根结点标注于标签位置一致 plottree (Intree, (0.5,1.0), ' ) plt.show () def plottree (mytree,parentpt,nodetext): Numleafs = Getnumleafs (mytree) depth = gettreedepth (myTre e) Firststr = Mytree.keys () [0] #第一个分类特征 centrpt = (Plottree.xoff + (1.0+float)) Numleafs, Plottree.yoff) Plotmidtext (Centrpt,parentpt,nodetext) #显示文本标签信息, the root node is empty plotnode (firststr,centrpt,parentpt,deci Sionnode) #打印标注特征信息 seconddict = mytree[firststr] Plottree.yoff = plottree.yoff-1.0/plottree.totald# Adjusts the y-direction position of the next sub-number For key in seconddict.keYS (): If Type (Seconddict[key]) ==dict:plottree (SECONDDICT[KEY],CENTRPT,STR (key)) Else: #画出结点即可 Plottree.xoff = Plottree.xoff + 1.0/plottree.totalw plotnode (Seconddict[key], (plottree.xoff,plottree.yo FF), Centrpt,leafnode) Plotmidtext ((Plottree.xoff,plottree.yoff), Centrpt,str (key)) Plottree.yoff = PlotTree.y off+1.0/plottree.totald# returns the upper Y-component Height mytree = retrievetree (1) #print gettreedepth (mytree) CRE due to recursion returning to the previous layer Ateplot (Mytree)
Machine learning Combat-decision Tree (II)