决策树框架:

 # coding=utf-8
import matplotlib.pyplot as plt decisionNode = dict(boxstyle='sawtooth', fc='')
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 getNumLeafs(myTree):
numLeafs = 0
firstStr = list(myTree.keys())[0]
secondDict = myTree[firstStr]
for key in secondDict:
if (type(secondDict[key]).__name__ == 'dict'):
numLeafs += getNumLeafs(secondDict[key])
else:
numLeafs += 1
return numLeafs def getTreeDepth(myTree):
maxDepth = 0
firstStr = list(myTree.keys())[0]
secondDict = myTree[firstStr]
for key in secondDict:
if (type(secondDict[key]).__name__ == 'dict'):
thisDepth = 1 + getTreeDepth((secondDict[key]))
else:
thisDepth = 1
if thisDepth > maxDepth: maxDepth = thisDepth
return maxDepth def retrieveTree(i):
# 预先设置树的信息
listOfTree = []
return listOfTree[i] def createPlot(inTree):
fig = plt.figure(1, facecolor='white')
fig.clf()
axprops = dict(xticks=[], yticks=[])
createPlot.ax1 = plt.subplot(111, frameon=False, **axprops)
plotTree.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.title('kaifeng.58.com\n')
plt.show() 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) def plotTree(myTree, parentPt, nodeTxt):
numLeafs = getNumLeafs(myTree)
depth = getTreeDepth(myTree)
firstStr = list(myTree.keys())[0]
cntrPt = (plotTree.xOff + (1.0 + float(numLeafs)) / 2.0 / plotTree.totalW, \
plotTree.yOff)
plotMidText(cntrPt, parentPt, nodeTxt)
plotNode(firstStr, cntrPt, parentPt, decisionNode)
secondDict = myTree[firstStr]
plotTree.yOff = plotTree.yOff - 1.0 / plotTree.totalD
for key in secondDict:
if type(secondDict[key]).__name__ == 'dict':
plotTree(secondDict[key], cntrPt, str(key))
else:
plotTree.xOff = plotTree.xOff + 1.0 / plotTree.totalW
plotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), \
cntrPt, leafNode)
plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))
plotTree.yOff = plotTree.yOff + 1.0 / plotTree.totalD if __name__ == '__main__':
myTree = retrieveTree(2)
createPlot(myTree)

构造信息:

  [{'no surfacing': {0: 'no', 1: {'flipper': {0: 'no', 1: 'yes'}}}},
{'no surfacing': {0: 'no', 1: {'flipper': {0: {'head': {0: 'no', 1: 'yes'}}, 1: 'no'}}}},
{'House prices <= 2000': {
1: {'Room size >= 50': {1: 'Yes', 0: 'No'}}, 0: 'No'}}]

结果:

Python爬虫(三)——开封市58同城出租房决策树构建的更多相关文章

  1. Python爬虫(四)——开封市58同城数据模型训练与检测

    前文参考: Python爬虫(一)——开封市58同城租房信息 Python爬虫(二)——对开封市58同城出租房数据进行分析 Python爬虫(三)——对豆瓣图书各模块评论数与评分图形化分析 数据的构建 ...

  2. Python爬虫(二)——对开封市58同城出租房数据进行分析

    出租房面积(area) 出租房价格(price) 对比信息 代码 import matplotlib as mpl import matplotlib.pyplot as plt import pan ...

  3. Python爬虫(一)——开封市58同城租房信息

    代码: # coding=utf-8 import sys import csv import requests from bs4 import BeautifulSoup reload(sys) s ...

  4. 养只爬虫当宠物(Node.js爬虫爬取58同城租房信息)

    先上一个源代码吧. https://github.com/answershuto/Rental 欢迎指导交流. 效果图 搭建Node.js环境及启动服务 安装node以及npm,用express模块启 ...

  5. python3爬虫-爬取58同城上所有城市的租房信息

    from fake_useragent import UserAgent from lxml import etree import requests, os import time, re, dat ...

  6. 用Python写爬虫爬取58同城二手交易数据

    爬了14W数据,存入Mongodb,用Charts库展示统计结果,这里展示一个示意 模块1 获取分类url列表 from bs4 import BeautifulSoup import request ...

  7. python 爬虫入门----案例爬取上海租房图片

    前言 对于一个net开发这爬虫真真的以前没有写过.这段时间学习python爬虫,今天周末无聊写了一段代码爬取上海租房图片,其实很简短就是利用爬虫的第三方库Requests与BeautifulSoup. ...

  8. python爬虫(三)

    Requests模块 这个库的标准文档有个极其幽默的地方就是它的中文翻译,我就截取个开头部分,如下图: 是不是很搞笑,在正文中还有许多,管中窥豹,可见一斑.通过我的使用,感觉Requests库的确是给 ...

  9. Python爬虫(三)爬淘宝MM图片

    直接上代码: # python2 # -*- coding: utf-8 -*- import urllib2 import re import string import os import shu ...

随机推荐

  1. TTL是什么意思?

    TTL是一个ip协议的值,它告诉网络,数据包在网络中的时间是否太长而应被丢弃.有很多原因使包在一定时间内不能被传递到目的地. TTL 的初值通常是系统缺省值,是包头中的8位的域.TTL的最初设想是确定 ...

  2. 【CF666C】Codeword 结论题+暴力

    [CF666C]Codeword 题意:一开始有一个字符串s,有m个事件,每个事件形如: 1.用一个新的字符串t来替换s2.给出n,问有多少个长度为n的小写字母组成的字符串满足包含s作为其一个子序列? ...

  3. Solve Error: 'NSInvalidArgumentException', reason: '-[UITableView mas_makeConstraints:]: unrecognized selector sent to instance 0x7fa5c402fa00'

    下面是iOS开发用第三方库可能出现的错误,及其解决方法: 1. 'NSInvalidArgumentException', reason: '-[UITableView mas_makeConstra ...

  4. APK使用APKTOOL命令行进行解包和重新打包

    在刚开始安卓逆向时遇到了一个很坑爹的问题,就是对apk进行反编译后不能重新打包, 查了一些资料,这里就简单说一下用apktool进行解包和打包的整个过程. 首先把一个apk和apktool放到同一目录 ...

  5. react useRef()函数

    "ref"对象是一个通用容器,其current属性是可变的 保存dom function Test() { const t = useRef(null); useEffect(() ...

  6. Git Bash上传文件

    今天通过Git Bash上传了一个项目(之前是通过Github Desk上传的),操作命令如下: 在目录下shift+右键打开Git Bash 1.git init 2.git add *.py 3. ...

  7. javascript的数组之find()

    find()方法返回数组中第一个满足回调函数测试的第一个元素的值.否则返回undefined const arr1 = [1, 2, 3, 4, 6, 9]; let found = arr1.fin ...

  8. 悬线法 || BZOJ 1057: [ZJOI2007]棋盘制作 || Luogu P1169 [ZJOI2007]棋盘制作

    题面:P1169 [ZJOI2007]棋盘制作 题解: 基本是悬线法板子,只是建图判断时有一点点不同. 代码: #include<cstdio> #include<cstring&g ...

  9. 线段树 || BZOJ1756: Vijos1083 小白逛公园 || P4513 小白逛公园

    题面:小白逛公园 题解: 对于线段树的每个节点除了普通线段树该维护的东西以外,额外维护lsum(与左端点相连的最大连续区间和).rsum(同理)和sum……就行了 代码: #include<cs ...

  10. svn和NetBeans一起使用造成svn老是死锁, database is locked

    其实我一直没发现,又一次我以为需要写些前端吧NetBeans关了,使用HBuilder写vue,因为内存只有8G,乱七八糟的一些东西一开,只剩20%左右,我就把 NetBeans关了,发现更新svn的 ...