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 = 0 18 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 += 1 25 return numLeafs 26 27 28 def getTreeDepth(myTree): 29 maxDepth = 0 30 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 = 1 37 if thisDepth > maxDepth: maxDepth = thisDepth 38 return maxDepth 39 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.0 56 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.totalD 77 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.totalW 82 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.totalD 86 87 88 if __name__ == '__main__': 89 myTree = retrieveTree(2) 90 createPlot(myTree)
构造信息:
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'}}]
结果:
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Python爬虫(二)——对开封市58同城出租房数据进行分析
出租房面积(area) 出租房价格(price) 对比信息 代码 1 import matplotlib as mpl 2 import matplotlib.pyplot as plt 3 import pandas as pad 4 import seaborn as sns 5 import numpy as np 6 7 sns.set_style('dark') 8 kf = pad.read_csv('kf.csv') 9 10 def sinplotone(): 11 fig,ax = plt.subplots() 12 ax.violinplot(kf['price']) 13 plt.show() 14 15 def sinplottwo(): 16 sns.set_style('whitegrid') 17 sns.boxplot(kf['price'],palette='deep') 18 # sns.despine(left=True) 19 plt.show() 20 21 def sinplotthree(): 22 sns....
- 下一篇
Java面试前需要了解的东西
一、前言 只有光头才能变强 回顾前面: 广州三本找Java实习经历 上一篇写了自己面试的经历和一些在面试的时候遇到的题目(笔试题和面试题)。 我在面试前针对Java基础也花了不少的时间,期间也将自己写过的博文粗略地刷了一遍,同时也在网上找了不少比较好的资料(部分是没看完的)。在这里给大家分享一下~~~ 这是我刷完自己写过的笔记写的一篇文章导航,针对于PC端的:https://zhongfucheng.bitcron.com/post/shou-ji/pcduan-wen-zhang-dao-hang。比较重要的知识点也画了思维导图,后面新发的文章也会补充上去的。 可能会感兴趣的资料: 思维导图原图:https://zhongfucheng.bitcron.com/post/shou-ji/nao-tu-da-quan 视频网盘:https://zhongfucheng.bitcron.com/post/shou-ji/shi-pin-wang-pan 二、在学习的时候整理的面试题 之前在学习或者整理知识点的时候也喜欢去找找面试题看,以下是我个人整理的面试题: 2.1集合 ArrayLi...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- CentOS8编译安装MySQL8.0.19
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7