【数据挖掘】分类之decision tree

1. ID3 算法

ID3 算法是一种典型的决策树(decision tree)算法,C4.5, CART都是在其基础上发展而来。决策树的叶子节点表示类标号,非叶子节点作为属性测试条件。从树的根节点开始,将测试条件用于检验记录,根据测试结果选择恰当的分支;直至到达叶子节点,叶子节点的类标号即为该记录的类别。

ID3采用信息增益(information gain)作为分裂属性的度量,最佳分裂等价于求解最大的信息增益。

信息增益=parent节点熵 - 带权的子女节点的熵

ID3算法流程如下:

1.如果节点的所有类标号相同,停止分裂;

2.如果没有feature可供分裂,根据多数表决确定该节点的类标号,并停止分裂;

3.选择最佳分裂的feature,根据选择feature的值逐一进行分裂;递归地构造决策树。

源代码(从[1]中拿过来):

from math import log
import operator
import matplotlib.pyplot as plt def calcEntropy(dataSet):
"""calculate the shannon entropy"""
numEntries=len(dataSet)
labelCounts={}
for entry in dataSet:
entry_label=entry[-1]
if entry_label not in labelCounts:
labelCounts[entry_label]=0
labelCounts[entry_label]+=1 entropy=0.0
for key in labelCounts:
prob=float(labelCounts[key])/numEntries
entropy-=prob*log(prob,2) return entropy def createDataSet():
dataSet = [[1, 1, 'yes'],
[1, 1, 'yes'],
[1, 0, 'no'],
[0, 1, 'no'],
[0, 1, 'no']]
labels = ['no surfacing','flippers']
return dataSet, labels def splitDataSet(dataSet,axis,pivot):
"""split dataset on feature"""
retDataSet=[]
for entry in dataSet:
if entry[axis]==pivot:
reduced_entry=entry[:axis]
reduced_entry.extend(entry[axis+1:])
retDataSet.append(reduced_entry)
return retDataSet def bestFeatureToSplit(dataSet):
"""chooose the best feature to split """
numFeatures=len(dataSet[0])-1
baseEntropy=calcEntropy(dataSet)
bestInfoGain=0.0; bestFeature=-1
for axis in range(numFeatures):
#create unique list of class labels
featureList=[entry[axis] for entry in dataSet]
uniqueFeaList=set(featureList)
newEntropy=0.0
for value in uniqueFeaList:
subDataSet=splitDataSet(dataSet,axis,value)
prob=float(len(subDataSet))/len(dataSet)
newEntropy+=prob*calcEntropy(subDataSet)
infoGain=baseEntropy-newEntropy
#find the best infomation gain
if infoGain>bestInfoGain:
bestInfoGain=infoGain
bestFeature=axis
return bestFeature def majorityVote(classList):
"""take a majority vote"""
classCount={}
for vote in classList:
if vote not in classCount.keys():
classCount[vote]=0
classCount+=1
sortedClassCount=sorted(classCount.iteritems(),
key=operator.itemgetter(1),reverse=True)
return sortedClassCount[0][0] def createTree(dataSet,labels):
classList=[entry[-1] for entry in dataSet]
#stop when all classes are equal
if classList.count(classList[0])==len(classList):
return classList[0]
#when no more features, return majority vote
if len(dataSet[0])==1:
return majorityVote(classList) bestFeature=bestFeatureToSplit(dataSet)
bestFeatLabel=labels[bestFeature]
myTree={bestFeatLabel:{}}
del(labels[bestFeature])
subLabels=labels[:]
featureList=[entry[bestFeature] for entry in dataSet]
uniqueFeaList=set(featureList)
#split dataset according to the values of the best feature
for value in uniqueFeaList:
subDataSet=splitDataSet(dataSet,bestFeature,value)
myTree[bestFeatLabel][value]=createTree(subDataSet,subLabels)
return myTree

分类结果可视化

2. Referrence

[1] Peter Harrington, machine learning in action.

【数据挖掘】分类之decision tree(转载)的更多相关文章

  1. CART分类与回归树与GBDT(Gradient Boost Decision Tree)

    一.CART分类与回归树 资料转载: http://dataunion.org/5771.html        Classification And Regression Tree(CART)是决策 ...

  2. 机器学习算法实践:决策树 (Decision Tree)(转载)

    前言 最近打算系统学习下机器学习的基础算法,避免眼高手低,决定把常用的机器学习基础算法都实现一遍以便加深印象.本文为这系列博客的第一篇,关于决策树(Decision Tree)的算法实现,文中我将对决 ...

  3. 数据挖掘 决策树 Decision tree

    数据挖掘-决策树 Decision tree 目录 数据挖掘-决策树 Decision tree 1. 决策树概述 1.1 决策树介绍 1.1.1 决策树定义 1.1.2 本质 1.1.3 决策树的组 ...

  4. 用于分类的决策树(Decision Tree)-ID3 C4.5

    决策树(Decision Tree)是一种基本的分类与回归方法(ID3.C4.5和基于 Gini 的 CART 可用于分类,CART还可用于回归).决策树在分类过程中,表示的是基于特征对实例进行划分, ...

  5. (ZT)算法杂货铺——分类算法之决策树(Decision tree)

    https://www.cnblogs.com/leoo2sk/archive/2010/09/19/decision-tree.html 3.1.摘要 在前面两篇文章中,分别介绍和讨论了朴素贝叶斯分 ...

  6. Spark2 ML包之决策树分类Decision tree classifier详细解说

    所用数据源,请参考本人博客http://www.cnblogs.com/wwxbi/p/6063613.html 1.导入包 import org.apache.spark.sql.SparkSess ...

  7. 【分类算法】决策树(Decision Tree)

    (注:本篇博文是对<统计学习方法>中决策树一章的归纳总结,下列的一些文字和图例均引自此书~) 决策树(decision tree)属于分类/回归方法.其具有可读性.可解释性.分类速度快等优 ...

  8. 【机器学习实战】第3章 决策树(Decision Tree)

    第3章 决策树 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/ ...

  9. 决策树Decision Tree 及实现

    Decision Tree 及实现 标签: 决策树熵信息增益分类有监督 2014-03-17 12:12 15010人阅读 评论(41) 收藏 举报  分类: Data Mining(25)  Pyt ...

随机推荐

  1. 八. 输入输出(IO)操作7.文件的随机读写

    Java.io 包提供了 RandomAccessFile 类用于随机文件的创建和访问.使用这个类,可以跳转到文件的任意位置读写数据.程序可以在随机文件中插入数据,而不会破坏该文件的其他数据.此外,程 ...

  2. elasticsearch 分布式部署

    修改配置文件 /config/elasticsearch.yml 我用两台机器,内网地址分别为230 和 231 处理启动报错一: [2017-01-12T15:55:55,433][INFO ][o ...

  3. Apache压力(并发)测试工具ab的使用教程收集

    说明:用ab的好处,在处理多并发的情况下不用自己写线程模拟.其实这个世界除了LoadRunner之外还是有很多方案可以选择的. 官网: http://httpd.apache.org/(Apache服 ...

  4. 阿里云ECS在CentOS 6.9中使用Nginx提示:nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)的解决方法

    说明: 1.[::]:80这个是IPv6的地址. 2.阿里云截至到今天还不支持IPv6. 解决方式: 1.普通解决方式:开启IPv6的支持,不过这个方法在阿里云行不通. vim /etc/nginx/ ...

  5. kaptcha Java验证码

    原文:http://www.cnblogs.com/chizizhixin/p/5311619.html 在项目中经常会使用验证码,kaptcha 就一个非常不错的开源框架,分享下自己在项目中的使用: ...

  6. Camera setParameters(), getParameters(),unlock()三个方法之间的限制关系

    Camera mCamera = Camera.open(); // 第一次调用getParameters()需要在unlock()方法之前否则出现错误 Camera.Parameters param ...

  7. Manthan, Codefest 16 D. Fibonacci-ish(暴力)

    题目链接:点击打开链接 题意:给你n个数, 问最长的题目中定义的斐波那契数列.  思路:枚举開始的两个数, 由于最多找90次, 所以能够直接暴力, 用map去重.  注意, 该题卡的时间有点厉害啊. ...

  8. JS或jQuery获取当前屏幕宽度

    Javascript: 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: document.b ...

  9. shell中set命令

    set命令作用主要是显示系统中已经存在的shell变量,以及设置shell变量的新变量值.set命令不能够定义新的shell变量.如果要定义新的变量,可以使用declare命令以变量名=值的格式进行定 ...

  10. 2016.6.30 tomcat开启时,显示端口被占用,如何修改端口

    开启tomcat时,有时候会显示端口8080已占用,所以需要将端口改为其他值. 找到tomcat的server.xml文件,修改为8088,如图所示: