"Reading notes" machine learning combat-decision tree (2)

Source: Internet
Author: User

Here is an introduction to the previous decision tree algorithm.
We have learned the whole method of decision tree before, and have a clearer understanding of its construction process. This time, the reading notes focused on the application of decision trees and the use of matplotlib to draw a decision tree.

Drawing Decision Trees

Matplotlib provides an annotation tool annotations, which is very similar to MATLAB [but the person thinks that Matlab drawing is more convenient to operate], he is a very powerful tool.

First we draw a node of the decision tree:

# Coding=utf-8ImportMatplotlib.pyplot asPltdecisionnode = Dict (boxstyle="Sawtooth", fc="0.8") Leafnode = Dict (boxstyle="Round4", fc="0.8") Arrow_args = Dict (arrowstyle="<-") def plotnode(Nodetxt, Centerpt, PARENTPT, NodeType):CreatePlot.ax1.annotate (Nodetxt, XY=PARENTPT, xycoords=' axes fraction ', Xytext=centerpt, textcoords=' axes fraction ', va="Center", ha="Center", Bbox=nodetype, Arrowprops=arrow_args) def createplot():Fig = Plt.figure (1, facecolor=' White ') Plt.xlabel (' ABS ') Plt.xlabel (' C ') FIG.CLF () Createplot.ax1 = Plt.subplot (111, frameon=False) Plotnode (' A decision node ', (0.5,0.1), (0.1,0.5), Decisionnode) Plotnode (' A leaf node ', (0.8,0.1), (0.3,0.8), Leafnode) Plt.show ()

Run the above results in PowerShell to get a slice

Then we'll construct the note tree:

It is natural to think of the case where each node is constructed with an iteration. In fact, it does.
The depth of the note tree and the number of leaf nodes are calculated first to facilitate the beginning and end of the iteration

 def getnumleafs(mytree):Numleafs =0Firststr = Mytree.keys () [0] Seconddict = Mytree[firststr] forKeyinchSeconddict.keys ():#查看当前节点是否已经是叶子节点, if not then iterate again        ifType (Seconddict[key]). __name__==' Dict ': Numleafs + = Getnumleafs (Seconddict[key])Else: Numleafs + =1    returnNumleafs def gettreedepth(mytree):MaxDepth =0Firststr = Mytree.keys () [0] Seconddict = Mytree[firststr] forKeyinchSeconddict.keys ():#查看当前节点是否已经是叶子节点, if not then iterate again        ifType (Seconddict[key]). __name__==' Dict ': Thisdepth =1+ gettreedepth (Seconddict[key])Else: Thisdepth =1        ifthisdepth > maxdepth:maxdepth = thisdepthreturnMaxDepth

The following is the main function for drawing the note tree:

 def plotmidtext(cntrpt, PARENTPT, txtstring):Xmid = (parentpt[0]-cntrpt[0])/2.0+ cntrpt[0] Ymid = (parentpt[1]-cntrpt[1])/2.0+ cntrpt[1] CreatePlot.ax1.text (Xmid, Ymid, txtstring, va="Center", ha="Center", rotation= -) def plottree(mytree, PARENTPT, Nodetxt):#if The first key tells you feat is split onNumleafs = Getnumleafs (mytree)#this determines the x width of this treedepth = gettreedepth (mytree) firststr = Mytree.keys () [0]#the text label for this node should is thisCntrpt = (Plottree.xoff + (1.0+ float (NUMLEAFS))/2.0/PLOTTREE.TOTALW, Plottree.yoff) plotmidtext (Cntrpt, PARENTPT, Nodetxt) Plotnode (Firststr, CntrPt, ParentPt, Decisio Nnode) seconddict = mytree[firststr] Plottree.yoff = Plottree.yoff-1.0/plottree.totald forKeyinchSeconddict.keys ():ifType (Seconddict[key]). __name__==' Dict ':#test to see if the nodes is dictonaires, if not they is leaf nodesPlottree (Seconddict[key],cntrpt,str (key))#recursion        Else:#it ' s a leaf node print the leaf nodePlottree.xoff = Plottree.xoff +1.0/PLOTTREE.TOTALW Plotnode (Seconddict[key], (Plottree.xoff, Plottree.yoff), Cntrpt, Leafnode) plotMid Text ((Plottree.xoff, Plottree.yoff), Cntrpt, str (key)) Plottree.yoff = Plottree.yoff +1.0/plottree.totald#if You do get a dictonary know it's a tree, and the first element would be another dict def createplot(intree):Fig = Plt.figure (1, facecolor=' White ') FIG.CLF () Axprops = Dict (xticks=[], yticks=[]) Createplot.ax1 = Plt.subplot (111, frameon=False, **axprops)#no Ticks    #createPlot. ax1 = Plt.subplot (111, Frameon=false) #ticks for demo puropsesPLOTTREE.TOTALW = Float (Getnumleafs (intree)) Plottree.totald = float (gettreedepth (intree)) Plottree.xoff =-0.5/PLOTTREE.TOTALW; Plottree.yoff =1.0; Plottree (Intree, (0.5,1.0),"') Plt.show ()

Application of Decision Tree algorithm

The eye doctor decides which contact lens to choose for the patient according to the patient's condition. This is an expert system problem, you can use decision trees to classify the classification index.

Since it is time-consuming to generate decision trees, it is not necessary to regenerate a decision tree with each decision tree, which describes the method of pickle serialization to store an already generated decision tree.
The basic usage of the Pickle module is as follows.

Use the Pickle module to store decision Trees:

def storeTree(inputTree,filename):    import pickle    fw = open(filename,‘w‘)    pickle.dump(inputTree,fw)    fw.close()def grabTree(filename):    import pickle    fr = open(filename)    return pickle.load(fr)

The storage format is as follows:

Implementation steps

    1. Collect data: Provide text files
    2. Prepare data: Resolve tab-split data rows
    3. Profiling data: Using the Createplot () function to draw the final tree diagram
    4. Training algorithm: Using the Createtree () function
    5. Test algorithm: Whether the algorithm can be classified correctly according to the actual situation
    6. Use algorithm: Storage decision tree structure, next direct use

Next time you can use the decision tree to do some sorting tasks. This chapter decision tree Construction algorithm is ID3 algorithm, later will learn more ~ refueling ~

"Reading notes" machine learning combat-decision tree (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.