c语言sscanf函数的用法是什么
260
2022-09-20
Python爬虫(三)——开封市58同城出租房决策树构建
决策树框架:
1 # coding=utf-8 2 import matplotlib.pyplot as plt 3 4 decisionNode = dict(boxstyle='sawtooth', fc='10') 5 leafNode = dict(boxstyle='round4', fc='0.8') 6 arrow_args = dict(arrowstyle='<-') 7 8 9 def plotNode(nodeTxt, centerPt, parentPt, nodeType):10 createPlot.ax1.annotate(nodeTxt, xy=parentPt, xycoords='axes fraction', \11 xytext=centerPt, textcoords='axes fraction', \12 va='center', ha='center', bbox=nodeType, arrowprops \13 =arrow_args)14 15 16 def getNumLeafs(myTree):17 numLeafs = 018 firstStr = list(myTree.keys())[0]19 secondDict = myTree[firstStr]20 for key in secondDict:21 if (type(secondDict[key]).__name__ == 'dict'):22 numLeafs += getNumLeafs(secondDict[key])23 else:24 numLeafs += 125 return numLeafs26 27 28 def getTreeDepth(myTree):29 maxDepth = 030 firstStr = list(myTree.keys())[0]31 secondDict = myTree[firstStr]32 for key in secondDict:33 if (type(secondDict[key]).__name__ == 'dict'):34 thisDepth = 1 + getTreeDepth((secondDict[key]))35 else:36 thisDepth = 137 if thisDepth > maxDepth: maxDepth = thisDepth38 return maxDepth39 40 41 def retrieveTree(i):42 # 预先设置树的信息43 listOfTree = []44 return listOfTree[i]45 46 47 def createPlot(inTree):48 fig = plt.figure(1, facecolor='white')49 fig.clf()50 axprops = dict(xticks=[], yticks=[])51 createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)52 plotTree.totalW = float(getNumLeafs(inTree))53 plotTree.totalD = float(getTreeDepth(inTree))54 plotTree.xOff = -0.5 / plotTree.totalW;55 plotTree.yOff = 1.056 plotTree(inTree, (0.5, 1.0), '')57 plt.title('kaifeng.58.com\n')58 plt.show()59 60 61 def plotMidText(cntrPt, parentPt, txtString):62 xMid = (parentPt[0] - cntrPt[0]) / 2.0 + cntrPt[0]63 yMid = (parentPt[1] - cntrPt[1]) / 2.0 + cntrPt[1]64 createPlot.ax1.text(xMid, yMid, txtString)65 66 67 def plotTree(myTree, parentPt, nodeTxt):68 numLeafs = getNumLeafs(myTree)69 depth = getTreeDepth(myTree)70 firstStr = list(myTree.keys())[0]71 cntrPt = (plotTree.xOff + (1.0 + float(numLeafs)) / 2.0 / plotTree.totalW, \72 plotTree.yOff)73 plotMidText(cntrPt, parentPt, nodeTxt)74 plotNode(firstStr, cntrPt, parentPt, decisionNode)75 secondDict = myTree[firstStr]76 plotTree.yOff = plotTree.yOff - 1.0 / plotTree.totalD77 for key in secondDict:78 if type(secondDict[key]).__name__ == 'dict':79 plotTree(secondDict[key], cntrPt, str(key))80 else:81 plotTree.xOff = plotTree.xOff + 1.0 / plotTree.totalW82 plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), \83 cntrPt, leafNode)84 plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))85 plotTree.yOff = plotTree.yOff + 1.0 / plotTree.totalD86 87 88 if __name__ == '__main__':89 myTree = retrieveTree(2)90
构造信息:
1 [{'no surfacing': {0: 'no', 1: {'flipper': {0: 'no', 1: 'yes'}}}},2 {'no surfacing': {0: 'no', 1: {'flipper': {0: {'head': {0: 'no', 1: 'yes'}}, 1: 'no'}}}},3 {'House prices <= 2000': {4 1: {'Room size >= 50': {1: 'Yes', 0: 'No'}}, 0: 'No'}}]
结果:
作者: AntzUhl
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~