徒手写代码之《机器学习实战》-----决策树算法(2)(使用决策树预测隐形眼镜类型)
使用决策树预测隐形眼镜类型
说明:
将数据集文件 ‘lenses.txt’ 放在当前文件夹
from math import log
import operator
熵的定义
"""
这部分是在用代码计算香农熵公式,即用代码写公式并计算结果
"""
def calcShannonEnt(dataSet):#数据集行数numEntries = len(dataSet)#创建空字典记录yes or no的次数labelCounts = {}#开始用循环语句统计标签频数,该思路和KNN算法是一样的#the the number of unique elements and their occurancefor featVec in dataSet:currentLabel = featVec[-1]if currentLabel not in labelCounts.keys(): labelCounts[currentLabel] = 0labelCounts[currentLabel] += 1#初始值是0,然后根据公式,用循环语句开始计算shannonEnt = 0.0for key in labelCounts:#公式计算部分prob = float(labelCounts[key])/numEntriesshannonEnt -= prob * log(prob, 2) return shannonEnt
划分数据集: 按照给定特征划分数据集
#这个函数后面用的上,先看懂代码。后一个函数会让你看懂它的真正作用。
def splitDataSet(dataSet, axis, value):retDataSet = []for featVec in dataSet:if featVec[axis] == value:#去掉axis特征reducedFeatVec = featVec[:axis] reducedFeatVec.extend(featVec[axis+1:])retDataSet.append(reducedFeatVec)return retDataSet
选择最好的数据集划分方式
#该函数选取最优的特征,并返回最优特征的索引值
def chooseBestFeatureToSplit(dataSet):#特征数量#the last column is used for the labelsnumFeatures = len(dataSet[0]) - 1 #计算数据集的香农熵baseEntropy = calcShannonEnt(dataSet)#信息增益bestInfoGain = 0.0#最优特征的索引值,-1是随便设置的值,便于后续更新值bestFeature = -1#遍历所有特征for i in range(numFeatures): #获取dataSet的第i个所有特征featList = [example[i] for example in dataSet]#创建set集合{},元素不可重复uniqueVals = set(featList) #经验条件熵newEntropy = 0.0#计算信息增益for value in uniqueVals:#subDataSet划分后的子集subDataSet = splitDataSet(dataSet, i, value)#计算子集的概率prob = len(subDataSet)/float(len(dataSet))#根据公式计算经验条件熵newEntropy += prob * calcShannonEnt(subDataSet)#信息增益。calculate the info gain; ie reduction in entropyinfoGain = baseEntropy - newEntropy #计算信息增益。compare this to the best gain so farif (infoGain > bestInfoGain): #更新信息增益,找到最大的信息增益。if better than current best, set to bestbestInfoGain = infoGain #记录信息增益最大的特征的索引值bestFeature = ireturn bestFeature
多数表决法决定该叶子节点的分类
#这个函数是在下一个函数里面第二个停止条件时候用上的
"""
代码与第2章classify0部分的投票表决代码非常类似
"""
def majorityCnt(classList):classCount={}#统计classList中每个元素出现的次数for vote in classList:if vote not in classCount.keys(): classCount[vote] = 0classCount[vote] += 1#最后利用operator操作键值排序字典,并返回出现次数最多的分类名称sortedClassCount = sorted(classCount.items(), key=operator.itemgetter(1), reverse=True)#返回classList中出现次数最多的元素return sortedClassCount[0][0]
递归构建决策树
#核心程序,创建树
def createTree(dataSet, labels):#取出所有分类标签,yes or noclassList = [example[-1] for example in dataSet]#如果类别完全相同,则停止继续划分。这是第一个停止条件。if classList.count(classList[0]) == len(classList):return classList[0]#stop splitting when there are no more features in dataSe#如果特征都遍历完了,还是没有划分的很纯净,那么就取数量多一点的那个类别。这是第二个停止条件if len(dataSet[0]) == 1: return majorityCnt(classList)#选择最优特征bestFeat = chooseBestFeatureToSplit(dataSet)#最优特征的标签bestFeatLabel = labels[bestFeat]#根据最优特征的标签生成树myTree = {bestFeatLabel:{}}#删除已经使用的特征标签del(labels[bestFeat])#得到训练集中所有最优特征的属性值featValues = [example[bestFeat] for example in dataSet]#去掉重复的属性值uniqueVals = set(featValues)#遍历特征,创建决策树。"""代码遍历当前选择特征包含的所有属性值,在每个数据集划分上递归调用函数,createTree(),得到的返回值将被插入到字典变量myTree中,因此函数终止执行时,字典中将会嵌套很多代表叶子节点信息的字典数据。"""for value in uniqueVals:#copy all of labels, so trees don't mess up existing labelssubLabels = labels[:] myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value), subLabels)return myTree
使用决策树执行分类
def classify(inputTree, featLabels, testVec):#获取决策树结点firstStr = list(inputTree)[0] #下一个字典secondDict = inputTree[firstStr] #返回节点特征的索引值featIndex = featLabels.index(firstStr) for key in secondDict.keys():if testVec[featIndex] == key:if type(secondDict[key]).__name__ == 'dict':classLabel = classify(secondDict[key], featLabels, testVec)else: classLabel = secondDict[key]return classLabel
使用决策树预测隐形眼镜类型
#打开文本数据
fr = open('lenses.txt')
#解析数据
lenses = [inst.strip().split('\t') for inst in fr.readlines()]
lensesLabels = ['age', 'prescript', 'astigmatic', 'tearRate']
#训练算法,根据creatTree函数建树
lensesTree = createTree(lenses,lensesLabels)
print(lensesTree)
{'tearRate': {'reduced': 'no lenses', 'normal': {'astigmatic': {'yes': {'prescript': {'myope': 'hard', 'hyper': {'age': {'pre': 'no lenses', 'presbyopic': 'no lenses', 'young': 'hard'}}}}, 'no': {'age': {'pre': 'soft', 'presbyopic': {'prescript': {'myope': 'no lenses', 'hyper': 'soft'}}, 'young': 'soft'}}}}}}
使用决策树模型进行预测
lensesLabels = ['age', 'prescript', 'astigmatic', 'tearRate']
classify(lensesTree, lensesLabels, lenses[0][:-1])
'no lenses'
对 lenses 数据集所有数据进行决策树分类预测
preds = []
for i in range(len(lenses)):pred = classify(lensesTree, lensesLabels, lenses[i][:-1])preds.append(pred)
print(preds)
['no lenses', 'soft', 'no lenses', 'hard', 'no lenses', 'soft', 'no lenses', 'hard', 'no lenses', 'soft', 'no lenses', 'hard', 'no lenses', 'soft', 'no lenses', 'no lenses', 'no lenses', 'no lenses', 'no lenses', 'hard', 'no lenses', 'soft', 'no lenses', 'no lenses']
徒手写代码之《机器学习实战》-----决策树算法(2)(使用决策树预测隐形眼镜类型)相关推荐
- Python3:《机器学习实战》之决策树算法(3)预测隐形眼镜类型
Python3:<机器学习实战>之决策树算法(3)预测隐形眼镜类型 转载请注明作者和出处:http://blog.csdn.net/u011475210 代码地址:https://gith ...
- 决策树算法学习笔记(三)-预测隐形眼镜类型
#coding=utf-8 import matplotlib.pyplot as plt #定义文本框和箭头格式 decisionNode=dict(boxstyle="sawtooth& ...
- Python3:《机器学习实战》之决策算法(3)预测隐形眼镜类型
Python3:<机器学习实战>之决策树算法(3)预测隐形眼镜类型 转载请注明作者和出处:http://blog.csdn.net/u011475210 代码地址:https://gith ...
- 机器学习实战--决策树算法
决策树 决策树(decision tree)是一种基本的分类与回归方法.从判断模块引出的左右箭头称为分支,它可以达到另外一个判断模块或者终止模块.分类决策树模型是一种描述对实例进行分类的树形结构.决策 ...
- 《机器学习实战》学习笔记:绘制树形图使用决策树预测隐形眼镜类型
上一节实现了决策树,但只是使用包含树结构信息的嵌套字典来实现,其表示形式较难理解,显然,绘制直观的二叉树图是十分必要的.Python没有提供自带的绘制树工具,需要自己编写函数,结合Matplotlib ...
- 【python和机器学习入门2】决策树3——使用决策树预测隐形眼镜类型
参考博客:决策树实战篇之为自己配个隐形眼镜 (po主Jack-Cui,<--大部分内容转载自 参考书籍:<机器学习实战>--第三章3.4 <--决策树基础知识见前两篇 , 摘要 ...
- 机器学习实战-决策树算法
本篇决策树算法是依据ID3算法来的,所以在看之间建议先了解ID3算法:https://blog.csdn.net/qq_27396861/article/details/88226296 文章目录 一 ...
- 机器学习实战ch03: 使用决策树预测隐形眼镜类型
决策树的一般流程 1.收集数据 2.准备数据:树构造算法只适用标称型数据,因此数据值型数据必须离散化 3.分析数据 4.训练算法 5.测试数据 6.使用算法 决策树的优点 1.数据形式非常容易理解 2 ...
- 决策树实战2-使用决策树预测隐形眼镜类型
这里是3.x版本的Python,对代码做了一些修改. 其中画图的函数直接使用的是原代码中的函数,也做了一些修改. 书本配套的数据和2.7版本的源码可以在这里获取 :https://www.mannin ...
最新文章
- PHP-Curl模拟HTTP请求
- python文件传输模块_如何将python对象从一个文件模块传递到另一个文件模块?
- jsp指令元素与动作元素
- 产生线程安全的原因(1)(操作系统)
- 在Spring中使用JDBCJobStore配置Quartz
- linux内核udp校验和计算函数,Linux 内核IP和UDP检验和计算
- 给网站文字添加图标-Font Awesome
- SYSTEM INSTRUCTIONS | 系统指令
- 活在贫困线之下的开源软件项目——开源的可持续性斗争
- canal 使用注意事项
- 嵌入式论文3000字_普通期刊发表论文费用是多少
- Leetcode:Longest Substring Without Repeating Characters分析和实现
- 一个简单的Java Web项目
- OMRON欧姆龙驱动器维修R88D-KT06F-Z过压故障处理
- Timesten安装
- 三维点云课程(一)——点云基础介绍
- PNAS | 人类线粒体的双亲遗传模式
- 【GoCN酷Go推荐】protobuf生成Go代码插件gogo/protobuf
- 什么是TPS,什么是QPS?
- 白杨SEO:从百度一下到抖音搜索关键词进行查找信息,挖掘用户需求的新机会你知道吗?