【数据挖掘】分类之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(转载)的更多相关文章
- CART分类与回归树与GBDT(Gradient Boost Decision Tree)
一.CART分类与回归树 资料转载: http://dataunion.org/5771.html Classification And Regression Tree(CART)是决策 ...
- 机器学习算法实践:决策树 (Decision Tree)(转载)
前言 最近打算系统学习下机器学习的基础算法,避免眼高手低,决定把常用的机器学习基础算法都实现一遍以便加深印象.本文为这系列博客的第一篇,关于决策树(Decision Tree)的算法实现,文中我将对决 ...
- 数据挖掘 决策树 Decision tree
数据挖掘-决策树 Decision tree 目录 数据挖掘-决策树 Decision tree 1. 决策树概述 1.1 决策树介绍 1.1.1 决策树定义 1.1.2 本质 1.1.3 决策树的组 ...
- 用于分类的决策树(Decision Tree)-ID3 C4.5
决策树(Decision Tree)是一种基本的分类与回归方法(ID3.C4.5和基于 Gini 的 CART 可用于分类,CART还可用于回归).决策树在分类过程中,表示的是基于特征对实例进行划分, ...
- (ZT)算法杂货铺——分类算法之决策树(Decision tree)
https://www.cnblogs.com/leoo2sk/archive/2010/09/19/decision-tree.html 3.1.摘要 在前面两篇文章中,分别介绍和讨论了朴素贝叶斯分 ...
- Spark2 ML包之决策树分类Decision tree classifier详细解说
所用数据源,请参考本人博客http://www.cnblogs.com/wwxbi/p/6063613.html 1.导入包 import org.apache.spark.sql.SparkSess ...
- 【分类算法】决策树(Decision Tree)
(注:本篇博文是对<统计学习方法>中决策树一章的归纳总结,下列的一些文字和图例均引自此书~) 决策树(decision tree)属于分类/回归方法.其具有可读性.可解释性.分类速度快等优 ...
- 【机器学习实战】第3章 决策树(Decision Tree)
第3章 决策树 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/ ...
- 决策树Decision Tree 及实现
Decision Tree 及实现 标签: 决策树熵信息增益分类有监督 2014-03-17 12:12 15010人阅读 评论(41) 收藏 举报 分类: Data Mining(25) Pyt ...
随机推荐
- tyvj——P1002 谁拿了最多奖学金
P1002 谁拿了最多奖学金 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2005复赛提高组第一题 描述 某校的惯例是在每学期的期末考试之后发 ...
- POJ2337 Catenyms(欧拉通路的求解)
Catenyms Time Limit: 1000MS Memory Limi ...
- android利用adb修改手机的分辨率和dpi
在android开发过程中,适配更多的适配是必不可少的一步,而每次测试适配时,要么购买设配,要么模拟器,买设配太花钱,模拟器太占内存,不过幸好还可以通过修改手机的size(分辨率)和density来进 ...
- linux下内存释放问题
参考: http://blog.sina.com.cn/s/blog_3cba7ec10100gk4k.html http://blog.sina.com.cn/s/blog_3cba7ec10100 ...
- C#中SortedList类的使用
C#中SortedList类 命名空间:System.Collections 程序集:mscorlib(在mscorlib.dll中) 语法:public class SortedList : IDi ...
- [转载]使用java.lang.Process类的简单例子
FROM: http://segmentfault.com/blog/lidonghao/1190000000372192 ProcessBuilder类是J2SE 1.5在java.lang中新添加 ...
- window命令
查看端口占用命令: 开始--运行--cmd 进入命令提示符 输入netstat -aon 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在 ...
- ubuntu apt-get install xxx时一直报错E: Unable to locate package xxxxxxx
$ sudo add-apt-repository main $ sudo add-apt-repository universe $ sudo add-apt-repository restrict ...
- python中MySQLdb模块用法实例
篇文章主要介绍了python中MySQLdb模块用法,以实例形式详细讲述了MySQLdb模块针对MySQL数据库的各种常见操作方法,非常具有实用价值,需要的朋友可以参考下 本文实例讲述了python中 ...
- vector list map 遍历删除指定元素
#include <stdio.h> #include <stdint.h> #include <vector> #include <list> #in ...