资源简介
python实现机器学习之决策树分类算法,简单易学,而且可直接运行。
代码片段和文件信息
‘‘‘
Created on Oct 14 2010
@author: Peter Harrington
‘‘‘
import matplotlib.pyplot as plt
decisionNode = dict(boxstyle=“sawtooth“ fc=“0.8“)
leafNode = dict(boxstyle=“round4“ fc=“0.8“)
arrow_args = dict(arrowstyle=“<-“)
def getNumLeafs(myTree):
numLeafs = 0
firstStr = myTree.keys()[0]
secondDict = myTree[firstStr]
for key in secondDict.keys():
if type(secondDict[key]).__name__==‘dict‘:#test to see if the nodes are dictonaires if not they are leaf nodes
numLeafs += getNumLeafs(secondDict[key])
else: numLeafs +=1
return numLeafs
def getTreeDepth(myTree):
maxDepth = 0
firstStr = myTree.keys()[0]
secondDict = myTree[firstStr]
for key in secondDict.keys():
if type(secondDict[key]).__name__==‘dict‘:#test to see if the nodes are dictonaires if not they are leaf nodes
thisDepth = 1 + getTreeDepth(secondDict[key])
else: thisDepth = 1
if thisDepth > maxDepth: maxDepth = thisDepth
return maxDepth
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 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=30)
def plotTree(myTree parentPt nodeTxt):#if the first key tells you what feat was split on
numLeafs = getNumLeafs(myTree) #this determines the x width of this tree
depth = getTreeDepth(myTree)
firstStr = myTree.keys()[0] #the text label for this node should be this
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.keys():
if type(secondDict[key]).__name__==‘dict‘:#test to see if the nodes are dictonaires if not they are leaf nodes
plotTree(secondDict[key]cntrPtstr(key)) #recursion
else: #it‘s a leaf node print the leaf node
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 you do get a dictonary you know it‘s a tree and the first element will 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 ti
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 101 2010-10-16 09:36 决策树+python\classifierStorage.txt
文件 795 2012-01-09 21:40 决策树+python\lenses.txt
文件 3911 2012-01-09 21:39 决策树+python\treePlotter.py
文件 3399 2012-01-09 21:39 决策树+python\treePlotter.pyc
文件 4170 2011-12-11 20:40 决策树+python\trees.py
文件 3692 2012-01-09 21:06 决策树+python\trees.pyc
目录 0 2018-07-23 10:12 决策树+python
----------- --------- ---------- ----- ----
16068 7
相关资源
- 文本分类算法LDA
- 改进的SIR模型评估k个重要点算法(
- 算法基础第五版 Foundation Of Algorithms
- 请求分页存储管理(操作系统课设)
- Iterative Bagging和MultiBoosting 算法python实
- Python3 实现SM3国产哈希算法
- 多目标区域的图像分割算法
- fancyimpute 在 python 中,实现了多元插值
- python实现VRPTW求解禁忌搜索算法
- python实现可视域算法
- Python实现朴素贝叶斯算法文本分类器
- 模式识别ISODATA算法
- Graph Cut图像分割算法——Python+Opencv实
- FP_Growth算法python实现.rar.rar
- 多目标优化算法(一)NSGA2python版
- 西电数据挖掘作业——关联规则apri
- GA-BP算法的python实现
- 朴素贝叶斯算法python底层代码
- 手写体数字识别原始数据和贝叶斯代
- 用A*算法解决TSP问题
- 模拟退火-遗传算法 34省会城市TSP问题
- 利用贝叶斯算法实现垃圾邮件分类
- 数据挖掘十大算法源代码Python)
- python实现扫描线填充算法,可以画凹
- A*算法解决十五数码问题Python程序、报
- 基于Python实现的Pagerank算法
- RSA算法的纯Python实现源码
- 《推荐系统实践》 程序实现 —— 2
- 基于标签的用户协同算法python
- python内置K-means聚类算法对鸢尾花数据
评论
共有 条评论