参考网址:https://www.cnblogs.com/further-further-further/p/9429257.html

ID3算法

最优决策树生成

-- coding: utf-8 --

"""
Created on Thu Aug 2 17:09:34 2018
决策树ID3的实现
@author: weixw
"""
from math import log
import operator

原始数据

def createDataSet():
dataSet = [[1, 1, 1,1,'yes'],
[1, 1, 0,0,'yes'],
[1, 0, 1,1,'no'],
[0, 1, 0,1,'yes'],
[0, 1, 1,0,'yes'],
[1, 1, 1, 1, 'yes'],
[1, 1, 0, 0, 'no'],
[1, 0, 1, 1, 'no'],
[0, 1, 0, 1, 'no'],
[0, 1, 1, 0, 'no']]
labels = ['no surfacing','flippers','people','day']
return dataSet, labels

多数表决器

列中相同值数量最多为结果

def majorityCnt(classList):
classCounts = {}
for value in classList:
if (value not in classCounts.keys()):
classCounts[value] = 0
classCounts[value] += 1
sortedClassCount = sorted(classCounts.iteritems(), key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0]

划分数据集

dataSet:原始数据集

axis:进行分割的指定列索引

value:指定列中的值

def splitDataSet(dataSet, axis, value):
retDataSet = []
for featDataVal in dataSet:
if featDataVal[axis] == value:
# 下面两行去除某一项指定列的值,很巧妙有没有
reducedFeatVal = featDataVal[:axis]
reducedFeatVal.extend(featDataVal[axis + 1:])
retDataSet.append(reducedFeatVal)
return retDataSet

计算香农熵

def calcShannonEnt(dataSet):
# 数据集总项数
numEntries = len(dataSet)
# 标签计数对象初始化
labelCounts = {}
for featDataVal in dataSet:
# 获取数据集每一项的最后一列的标签值
currentLabel = featDataVal[-1]
# 如果当前标签不在标签存储对象里,则初始化,然后计数
if currentLabel not in labelCounts.keys():
labelCounts[currentLabel] = 0
labelCounts[currentLabel] += 1
# 熵初始化
shannonEnt = 0.0
# 遍历标签对象,求概率,计算熵
for key in labelCounts.keys():
prop = labelCounts[key] / float(numEntries)
shannonEnt -= prop * log(prop, 2)
return shannonEnt

选出最优特征列索引

def chooseBestFeatureToSplit(dataSet):
# 计算特征个数,dataSet最后一列是标签属性,不是特征量
numFeatures = len(dataSet[0]) - 1
# 计算初始数据香农熵
baseEntropy = calcShannonEnt(dataSet)
# 初始化信息增益,最优划分特征列索引
bestInfoGain = 0.0
bestFeatureIndex = -1
for i in range(numFeatures):
# 获取每一列数据
featList = [example[i] for example in dataSet]
# 将每一列数据去重
uniqueVals = set(featList)
newEntropy = 0.0
for value in uniqueVals:
subDataSet = splitDataSet(dataSet, i, value)
# 计算条件概率
prob = len(subDataSet) / float(len(dataSet))
# 计算条件熵
newEntropy += prob * calcShannonEnt(subDataSet)
# 计算信息增益
infoGain = baseEntropy - newEntropy
if (infoGain > bestInfoGain):
bestInfoGain = infoGain
bestFeatureIndex = i
return bestFeatureIndex

决策树创建

def createTree(dataSet, labels):
# 获取标签属性,dataSet最后一列,区别于labels标签名称
classList = [example[-1] for example in dataSet]
# 树极端终止条件判断
# 标签属性值全部相同,返回标签属性第一项值
if classList.count(classList[0]) == len(classList):
return classList[0]
# 只有一个特征(1列)
if len(dataSet[0]) == 1:
return majorityCnt(classList)
# 获取最优特征列索引
bestFeatureIndex = chooseBestFeatureToSplit(dataSet)
# 获取最优索引对应的标签名称
bestFeatureLabel = labels[bestFeatureIndex]
# 创建根节点
myTree = {bestFeatureLabel: {}}
# 去除最优索引对应的标签名,使labels标签能正确遍历
del (labels[bestFeatureIndex])
# 获取最优列
bestFeature = [example[bestFeatureIndex] for example in dataSet]
uniquesVals = set(bestFeature)
for value in uniquesVals:
# 子标签名称集合
subLabels = labels[:]
# 递归
myTree[bestFeatureLabel][value] = createTree(splitDataSet(dataSet, bestFeatureIndex, value), subLabels)
return myTree

获取分类结果

inputTree:决策树字典

featLabels:标签列表

testVec:测试向量 例如:简单实例下某一路径 [1,1] => yes(树干值组合,从根结点到叶子节点)

def classify(inputTree, featLabels, testVec):
# 获取根结点名称,将dict转化为list
firstSide = list(inputTree.keys())
# 根结点名称String类型
firstStr = firstSide[0]
# 获取根结点对应的子节点
secondDict = inputTree[firstStr]
# 获取根结点名称在标签列表中对应的索引
featIndex = featLabels.index(firstStr)
# 由索引获取向量表中的对应值
key = testVec[featIndex]
# 获取树干向量后的对象
valueOfFeat = secondDict[key]
# 判断是子结点还是叶子节点:子结点就回调分类函数,叶子结点就是分类结果
# if type(valueOfFeat).__name__=='dict': 等价 if isinstance(valueOfFeat, dict):
if isinstance(valueOfFeat, dict):
classLabel = classify(valueOfFeat, featLabels, testVec)
else:
classLabel = valueOfFeat
return classLabel

将决策树分类器存储在磁盘中,filename一般保存为txt格式

def storeTree(inputTree, filename):
import pickle
fw = open(filename, 'wb+')
pickle.dump(inputTree, fw)
fw.close()

将瓷盘中的对象加载出来,这里的filename就是上面函数中的txt文件

def grabTree(filename):
import pickle
fr = open(filename, 'rb')
return pickle.load(fr)

决策树绘制

'''
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
# dict转化为list
firstSides = list(myTree.keys())
firstStr = firstSides[0]
secondDict = myTree[firstStr]
for key in secondDict.keys():
# 判断是否是叶子节点(通过类型判断,子类不存在,则类型为str;子类存在,则为dict)
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
# dict转化为list
firstSides = list(myTree.keys())
firstStr = firstSides[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)
firstSides = list(myTree.keys())
firstStr = firstSides[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], cntrPt, str(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 ticks
# createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses
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.show()

绘制树的根节点和叶子节点(根节点形状:长方形,叶子节点:椭圆形)

def createPlot():

fig = plt.figure(1, facecolor='white')

fig.clf()

createPlot.ax1 = plt.subplot(111, frameon=False) #ticks for demo puropses

plotNode('a decision node', (0.5, 0.1), (0.1, 0.5), decisionNode)

plotNode('a leaf node', (0.8, 0.1), (0.3, 0.8), leafNode)

plt.show()

def 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]

thisTree = retrieveTree(0)

createPlot(thisTree)

createPlot()

myTree = retrieveTree(0)

numLeafs =getNumLeafs(myTree)

treeDepth =getTreeDepth(myTree)

print(u"叶子节点数目:%d"% numLeafs)

print(u"树深度:%d"%treeDepth)

测试代码

-- coding: utf-8 --

"""
Created on Fri Aug 3 19:52:10 2018

@author: weixw
"""
import Demo_1.myTrees as mt
import Demo_1.treePlotter as tp

测试

dataSet, labels = mt.createDataSet()

copy函数:新开辟一块内存,然后将list的所有值复制到新开辟的内存中

labels1 = labels.copy()

createTree函数中将labels1的值改变了,所以在分类测试时不能用labels1

myTree = mt.createTree(dataSet,labels1)

保存树到本地

mt.storeTree(myTree,'myTree.txt')

在本地磁盘获取树

myTree = mt.grabTree('myTree.txt')
print (u"决策树结构:%s"%myTree)

绘制决策树

print(u"绘制决策树:")
tp.createPlot(myTree)
numLeafs =tp.getNumLeafs(myTree)
treeDepth =tp.getTreeDepth(myTree)
print(u"叶子节点数目:%d"% numLeafs)
print(u"树深度:%d"%treeDepth)

测试分类 简单样本数据3列

labelResult =mt.classify(myTree,labels,[1,1,1,0])
print(u"[1,1] 测试结果为:%s"%labelResult)
labelResult =mt.classify(myTree,labels,[1,0,0,0])
print(u"[1,0] 测试结果为:%s"%labelResult)

决策树——ID3的更多相关文章

  1. 数据挖掘之决策树ID3算法(C#实现)

    决策树是一种非常经典的分类器,它的作用原理有点类似于我们玩的猜谜游戏.比如猜一个动物: 问:这个动物是陆生动物吗? 答:是的. 问:这个动物有鳃吗? 答:没有. 这样的两个问题顺序就有些颠倒,因为一般 ...

  2. 决策树ID3算法[分类算法]

    ID3分类算法的编码实现 <?php /* *决策树ID3算法(分类算法的实现) */ /* *求信息增益Grain(S1,S2) */ //-------------------------- ...

  3. javascript实现朴素贝叶斯分类与决策树ID3分类

    今年毕业时的毕设是有关大数据及机器学习的题目.因为那个时间已经步入前端的行业自然选择使用JavaScript来实现其中具体的算法.虽然JavaScript不是做大数据处理的最佳语言,相比还没有优势,但 ...

  4. 决策树--ID3 算法(一)

    Contents      1. 决策树的基本认识      2. ID3算法介绍      3. 信息熵与信息增益      4. ID3算法的C++实现 1. 决策树的基本认识    决策树是一种 ...

  5. 决策树(ID3、C4.5、CART)

    ID3决策树 ID3决策树分类的根据是样本集分类前后的信息增益. 假设我们有一个样本集,里面每个样本都有自己的分类结果. 而信息熵可以理解为:“样本集中分类结果的平均不确定性”,俗称信息的纯度. 即熵 ...

  6. 决策树---ID3算法(介绍及Python实现)

    决策树---ID3算法   决策树: 以天气数据库的训练数据为例. Outlook Temperature Humidity Windy PlayGolf? sunny 85 85 FALSE no ...

  7. 机器学习实战 -- 决策树(ID3)

    机器学习实战 -- 决策树(ID3)   ID3是什么我也不知道,不急,知道他是干什么的就行   ID3是最经典最基础的一种决策树算法,他会将每一个特征都设为决策节点,有时候,一个数据集中,某些特征属 ...

  8. 决策树ID3原理及R语言python代码实现(西瓜书)

    决策树ID3原理及R语言python代码实现(西瓜书) 摘要: 决策树是机器学习中一种非常常见的分类与回归方法,可以认为是if-else结构的规则.分类决策树是由节点和有向边组成的树形结构,节点表示特 ...

  9. 02-21 决策树ID3算法

    目录 决策树ID3算法 一.决策树ID3算法学习目标 二.决策树引入 三.决策树ID3算法详解 3.1 if-else和决策树 3.2 信息增益 四.决策树ID3算法流程 4.1 输入 4.2 输出 ...

随机推荐

  1. CF-Educational Codeforces Round 77 (Rated for Div. 2)(A-E题解)

    A. Heating (水题) 题目链接 大致思路: 因为是代价是平方,所以让每一个房间的大小平均即可,即最大和最小相差不超过一. 代码: #include<bits/stdc++.h> ...

  2. 长乐国庆集训Day5

    T1 方阵 题目 [题目描述] 小澳最近迷上了考古,他发现秦始皇的兵马俑布局十分有特点,热爱钻研的小澳打算在电脑上还原这个伟大的布局. 他努力钻研,发现秦始皇布置兵马俑是有一定规律的.兵马俑阵总共有n ...

  3. 长乐国庆集训Day4

    T1 一道数论神题 题目 [题目描述] LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,只有点权. LYK想把这个图删干净,它的方法是这样的.每次选择一个点,将它删 ...

  4. sparksql读取hive数据报错:java.lang.RuntimeException: serious problem

    问题: Caused by: java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: ...

  5. JAVA字符串比较,BigDecimal比较

    import java.math.BigDecimal; public class StrCompareTo { public static void main(String[] args) { Sy ...

  6. 通过创建一个简单的骰子游戏来探究 Python

    在我的这系列的第一篇文章 中, 我已经讲解如何使用 Python 创建一个简单的.基于文本的骰子游戏.这次,我将展示如何使用 Python 模块 Pygame 来创建一个图形化游戏.它将需要几篇文章才 ...

  7. Workerman简单开发示例实践(一)

    一.去官网下载workerman,地址:https://www.workerman.net/download,下载后解压任意文件夹. 二.在解压文件目录下新建http_test.php,输入如下代码: ...

  8. 英语Bisynes商务

    英语bisynes商务概念的提出是改革的产物,有一个演变的过程:贸易部--商业部.外贸部--内贸部--内贸局--商务部.是内外贸一体化的概念. 中文名:商务 外文名:Business,Bisynes商 ...

  9. 车间管理难?APS系统为你智能排程

    对 APS系统不熟或者不了解他的一些运行规则也是在实施项目中导致经常不能正常运行不可忽视的因素,对 APS系统的早期了解是整个项目实施运行的成功至关重要的因素. 如果不了解 APS潜在的因素和运行准则 ...

  10. Android 为TV端助力之解决ViewPager嵌套RecyclerView水平滑动问题

    public class MyViewPager extends ViewPager { private RecyclerView recyclerView; public MyViewPager(@ ...