python 由遞歸的dict構建樹的畫圖代碼


createPlot(mytree)方法實現。

其中myTree是一個字典,調用retrieveTree(0)可以獲得一個字典的樣式。

Last login: Thu Feb 23 19:07:53 on ttys000
B000000060143:~ zhanghao44$ ls
4        Desktop        IdeaProjects    Music        PycharmProjects
4a        Documents    Library        Pictures
CLionProjects    Downloads    Movies        Public
B000000060143:~ zhanghao44$ cd Desktop/
B000000060143:Desktop zhanghao44$ ls
help
machinelearninginaction
ps-dsp
終端
備份 
屏幕快照 2017-02-22 下午7.36.56.png
屏幕快照 2017-02-22 下午7.41.27.png
屏幕快照 2017-02-22 下午7.45.55.png
張浩出勤統計.xlsx
B000000060143:Desktop zhanghao44$ cd machinelearninginaction/
B000000060143:machinelearninginaction zhanghao44$ ls
Ch02        Ch05        Ch08        Ch11        Ch14
Ch03        Ch06        Ch09        Ch12        Ch15
Ch04        Ch07        Ch10        Ch13        README.rst
B000000060143:machinelearninginaction zhanghao44$ cd Ch03
B000000060143:Ch03 zhanghao44$ ls
classifierStorage.txt    treePlotter.py        trees.py
lenses.txt        treePlotter.pyc        trees.pyc
B000000060143:Ch03 zhanghao44$ vi trees.py
B000000060143:Ch03 zhanghao44$ vi treePlotter.py



































B000000060143:Ch03 zhanghao44$ vi treePlotter.py

    plotTree.totalD = float(getTreeDepth(inTree))
    plotTree.xOff = -0.5/plotTree.totalW; plotTree.yOff = 1.0;
    plotTree(inTree, (0.5,1.0), '')
    plt.show()

def createPlot1():
    fig = plt.figure(1, facecolor='white')
    fig.clf()
    createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
    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()

def retrieveTree(i):
    listOfTrees =[{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}},
                  {'no surfacing': {0: 'no', 1: {'flippers': {0: {'head': {0: 'no', 1: 'yes'}}, 1: 'no'}}}}
                  ]
    return listOfTrees[i]

#createPlot(thisTree)
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM