我们将用递归来构建分类器并使用 Matplotlib 绘制。分类器获取隐形眼镜处方的数据,并用尝试预测人们需要什么镜片。
需要用到的库

# -*- coding: utf-8 -*-from math import log
import operator
import matplotlib.pyplot as plt

主要代码

def calcShannonEnt(dataSet):numEntries = len(dataSet)labelCounts = {}for featVec in dataSet:currentLabel = featVec[-1]if currentLabel not in labelCounts.keys():labelCounts[currentLabel] = 0labelCounts[currentLabel] += 1shannonEnt = 0.0for key in labelCounts:prob = float(labelCounts[key]) / numEntriesshannonEnt -= prob * log(prob, 2)return shannonEntdef splitDataSet(dataSet, axis, value):retDataSet = []for featVec in dataSet:if featVec[axis] == value:reducedFeatVec = featVec[:axis]reducedFeatVec.extend(featVec[axis + 1:])retDataSet.append(reducedFeatVec)return retDataSetdef chooseBestFeatureToSplit(dataSet):numFeatures = len(dataSet[0]) - 1baseEntropy = calcShannonEnt(dataSet)bestInfoGain = 0.0bestFeature = -1for i in range(numFeatures):featList = [example[i] for example in dataSet]uniqueVals = set(featList)newEntropy = 0.0for value in uniqueVals:subDataSet = splitDataSet(dataSet, i, value)prob = len(subDataSet) / float(len(dataSet))newEntropy += prob * calcShannonEnt(subDataSet)infoGain = baseEntropy - newEntropyif (infoGain > bestInfoGain):bestInfoGain = infoGainbestFeature = ireturn bestFeaturedef majorityCnt(classList):classCount = {}for vote in classList:if vote not in classCount.keys():classCount[vote] = 0classCount[vote] += 1sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)return sortedClassCount[0][0]def createTree(dataSet, labels):classList = [example[-1] for example in dataSet]if classList.count(classList[0]) == len(classList):return classList[0]if len(dataSet[0]) == 1:return majorityCnt(classList)bestFeat = chooseBestFeatureToSplit(dataSet)bestFeatLabel = labels[bestFeat]myTree = {bestFeatLabel: {}}# print("0tree", myTree)del (labels[bestFeat])featValues = [example[bestFeat] for example in dataSet]uniqueVals = set(featValues)for value in uniqueVals:subLabels = labels[:]myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels)return myTreedef getNumLeafs(myTree):numLeafs = 0temp_keys = list(myTree.keys())firstStr = temp_keys[0]secondDict = myTree[firstStr]for key in secondDict.keys():if type(secondDict[key]).__name__ == 'dict':numLeafs += getNumLeafs(secondDict[key])else:numLeafs += 1return numLeafsdef getTreeDepth(myTree):maxDepth = 0firstStr = next(iter(myTree))secondDict = myTree[firstStr]for key in secondDict.keys():if type(secondDict[key]).__name__ == 'dict':thisDepth = 1 + getTreeDepth(secondDict[key])else:thisDepth = 1if thisDepth > maxDepth:maxDepth = thisDepthreturn maxDepthdef 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]decisionNode = dict(boxstyle="sawtooth", fc="0.8")
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 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):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.totalDfor key in secondDict.keys():if type(secondDict[key]).__name__ == 'dict':plotTree(secondDict[key], cntrPt, str(key))else:plotTree.xOff = plotTree.xOff + 1.0 / plotTree.totalWplotNode(secondDict[key], (plotTree.xOff, plotTree.yOff), cntrPt, leafNode)plotMidText((plotTree.xOff, plotTree.yOff), cntrPt, str(key))plotTree.yOff = plotTree.yOff + 1.0 / plotTree.totalDdef 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.totalWplotTree.yOff = 1.0plotTree(inTree, (0.5, 1.0), '')plt.show()def prodict():with open("lenses.txt", "rb") as fr:lenses = [inst.decode().strip().split('\t') for inst in fr.readlines()]lensesLabels = ['age', 'prescript', 'astigmatic', "tearRate"]lensesTree = createTree(lenses, lensesLabels)createPlot(lensesTree)return lensesTreedef classify(inputTree, featLabels, testVec):firstStr = list(inputTree.keys())[0]secondDict = inputTree[firstStr]featIndex = featLabels.index(firstStr)key = testVec[featIndex]valueOfFeat = secondDict[key]if isinstance(valueOfFeat, dict):classLabel = classify(valueOfFeat, featLabels, testVec)else:classLabel = valueOfFeatreturn classLabel

主函数如下

if __name__ == "__main__":mytree = prodict()labels = ['age', 'prescript', 'astigmatic', "tearRate"]result = classify(mytree, labels, ["presbyopic", "hyper", "yes", "normal"])if result == 'no lenses':print("视力良好")if result == 'soft':print("轻微近视")if result == 'hard':print("重度近视")

需要用到隐形眼镜数据集lenses.txt文件如下
young myope no reduced no lenses
young myope no normal soft
young myope yes reduced no lenses
young myope yes normal hard
young hyper no reduced no lenses
young hyper no normal soft
young hyper yes reduced no lenses
young hyper yes normal hard
pre myope no reduced no lenses
pre myope no normal soft
pre myope yes reduced no lenses
pre myope yes normal hard
pre hyper no reduced no lenses
pre hyper no normal soft
pre hyper yes reduced no lenses
pre hyper yes normal no lenses
presbyopic myope no reduced no lenses
presbyopic myope no normal no lenses
presbyopic myope yes reduced no lenses
presbyopic myope yes normal hard
presbyopic hyper no reduced no lenses
presbyopic hyper no normal soft
presbyopic hyper yes reduced no lenses
presbyopic hyper yes normal no lenses

将py文件与txt放于同目录下即可运行

机器学习实战-决策树预测隐形眼镜类型相关推荐

  1. 徒手写代码之《机器学习实战》-----决策树算法(2)(使用决策树预测隐形眼镜类型)

    使用决策树预测隐形眼镜类型 说明: 将数据集文件 'lenses.txt' 放在当前文件夹 from math import log import operator 熵的定义 "" ...

  2. 《机器学习实战》学习笔记:绘制树形图使用决策树预测隐形眼镜类型

    上一节实现了决策树,但只是使用包含树结构信息的嵌套字典来实现,其表示形式较难理解,显然,绘制直观的二叉树图是十分必要的.Python没有提供自带的绘制树工具,需要自己编写函数,结合Matplotlib ...

  3. python3.5《机器学习实战》学习笔记(五):决策树算法实战之预测隐形眼镜类型

    转载请注明作者和出处:http://blog.csdn.net/u013829973 系统版本:window 7 (64bit) 文章出现的所有代码在我的GitHub:https://github.c ...

  4. 03_使用决策树预测隐形眼镜类型

    使用决策树预测隐形眼镜类型 1.实验描述 使用Python编程,输入为隐形眼镜数据集,计算所有可能的特征的信息增益,选择最优的特征值划分数据集,进而递归地构建决策树.其中为了更加直观地呈现决策树,使用 ...

  5. 决策树(四):使用决策树预测隐形眼镜类型

    使用决策树预测隐形眼镜类型 介绍 代码部分 总结 介绍 本节我们将通过一个例子讲解决策树如何预测患者需要佩戴的隐形眼镜类型.使用小数据集 ,我们就可以利用决策树学到很多知识:眼科医生是如何判断患者需要 ...

  6. ID3构造决策树预测隐形眼镜类型(代码笔记)

    决策树可以从数据集合中提取出一系列规则,从而分类样本.它的优势是理解数据蕴含信息. 思想:利用信息增益(information gain)[度量数据集信息的方式-香农熵(entropy)]计算得出最好 ...

  7. 【python和机器学习入门2】决策树3——使用决策树预测隐形眼镜类型

    参考博客:决策树实战篇之为自己配个隐形眼镜 (po主Jack-Cui,<--大部分内容转载自 参考书籍:<机器学习实战>--第三章3.4 <--决策树基础知识见前两篇 , 摘要 ...

  8. 机器学习实战ch03: 使用决策树预测隐形眼镜类型

    决策树的一般流程 1.收集数据 2.准备数据:树构造算法只适用标称型数据,因此数据值型数据必须离散化 3.分析数据 4.训练算法 5.测试数据 6.使用算法 决策树的优点 1.数据形式非常容易理解 2 ...

  9. Educoder 机器学习 决策树使用之使用决策树预测隐形眼镜类型

    任务描述 相关知识 如何处理隐形眼镜数据集 编程要求 测试说明 任务描述 本关任务:编写一个例子讲解决策树如何预测患者需要佩戴的隐形眼镜类型.使用小数据集,我们就可以利用决策树学到很多知识:眼科医生是 ...

  10. 决策树实战2-使用决策树预测隐形眼镜类型

    这里是3.x版本的Python,对代码做了一些修改. 其中画图的函数直接使用的是原代码中的函数,也做了一些修改. 书本配套的数据和2.7版本的源码可以在这里获取 :https://www.mannin ...

最新文章

  1. Linux下配置rdate时间服务器
  2. Julia程序设计3 数组1 创建、初始化、属性与访问
  3. simple-android-flux,深入浅出Flux
  4. 1 week110的zookeeper的安装 + zookeeper提供少量数据的存储
  5. 詹金斯搭建_与詹金斯一起将GitLab中的Gradle项目发布到Artifactory
  6. clover config_clover的每一个小细节都给你弄得明明白白的
  7. 黄学长模拟day1 球的序列
  8. 基于FPGA的简易DDS信号发生器的设计与验证
  9. 再回首Java第二十二天
  10. 主力吸筹猛攻指标源码_通达信大于9000手大单指标公式,主力吸筹猛攻指标源码...
  11. android更改深色模式,安卓微信怎么切换深色模式
  12. FDTD Solutions初学笔记
  13. POJ 1008 玛雅日历
  14. java timezone id_java – 从SimpleTimeZone获取ZoneId
  15. 电大计算机专业英语形成性考试,电大资源网《管理英语1》形成性考核册作业题目和答案2018年...
  16. SpringBoot+Thymeleaf图片上传
  17. 计算机类学生考试系统,学生在线考试系统.doc
  18. 微机原理知识点总结10-12
  19. 做汽车工程师需要哪些计算机语言,做好汽车研发工程师,需要懂哪些?
  20. 锐捷NBR路由器 EWEB网管系统 远程命令执行漏洞 + GetShell

热门文章

  1. PCAN二次开发,用MFC发送一帧CAN消息
  2. 基于C51单片机的锂电池容量检测仪电压电流检测 原理图PCB程序设计
  3. 小程序UI框架推荐:Vant让你优雅的飞
  4. Python 爬虫 爬取视频
  5. 数据库备份的三种方式 不要再干掉数据库跑路啦~
  6. 苹果手表计算机功能键,认识Mac的功能键,最值得你记住的苹果电脑快捷键列表...
  7. 企业微信电脑版显示连不上服务器,钉钉环境部分Anroid手机,微信、企业微信pc版,登录失败的解决办法...
  8. Python代码对接抖音去水印提取API接口、TikTok去水印解析接口的源码
  9. css属性table
  10. opencv图像处理进阶——灰度直方图