ML-决策树
来源:互联网 发布:预装软件下载 编辑:程序博客网 时间:2024/04/28 05:26
决策树是一种进行类别递归分类的分类算法, 具体的原理就是构造一棵决策树,对所有相同的类别分别作为左右子节点(当然也可以是多树杈的树)
决策树的核心在于如何找到最优的特征值来对于数据集合进行分类。最好的方法就是用香浓定理了
香浓定理:
H 就代表了训练集合所有特征的熵,得到了熵之后,我们就可以获取最大信息增益的方法来选择最佳的划分特征值。
python代码:
def calcShannonEnt(dataSet): numEntries = len(dataSet) labelCounts = {} for featVec in dataSet: #the the number of unique elements and their occurance currentLabel = featVec[-1] if currentLabel not in labelCounts.keys(): labelCounts[currentLabel] = 0 labelCounts[currentLabel] += 1 shannonEnt = 0.0 for key in labelCounts: prob = float(labelCounts[key])/numEntries shannonEnt -= prob * log(prob,2) #log base 2 return shannonEnt
通过计算出的集合熵对分类特征做出选择(根据信息的增益)
def chooseBestFeatureToSplit(dataSet): numFeatures = len(dataSet[0]) - 1 #the last column is used for the labels baseEntropy = calcShannonEnt(dataSet) bestInfoGain = 0.0; bestFeature = -1 for i in range(numFeatures): #iterate over all the features featList = [example[i] for example in dataSet]#create a list of all the examples of this feature uniqueVals = set(featList) #get a set of unique values 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 #calculate the info gain; ie reduction in entropy if (infoGain > bestInfoGain): #compare this to the best gain so far bestInfoGain = infoGain #if better than current best, set to best bestFeature = i return bestFeature #returns an integerbestFeature就是当前划分的最佳划分特征值在训练集合中的index,从上面的代码可以看到,迭代判定每一个特征对于所有样本的香浓值得增益,
infoGain = baseEntropy - newEntropy
如果当前的特征可以样本划分的很干净, 那么该香浓值必定很小,从而使得信息增益最大。
注意:这里就表现出了用信息增益来作为划分的缺陷, 可以看到,如果当前的特征拥有很多可取值得时候, 必然导致当前的香浓值很大,导致增益的最大化,但是,未必这个特征就是最好的划分特征。针对这个问题,我们可以用CART来解决,这个我们之后会说到。
之后的决策树的建立就是在提取了这个特征值后的剩余集合中进行递归的特征值选择划分,算法就不实现了,网上很多(PS:以上代码也是参考网上大牛的实现)
阅读全文
0 0
- ML-决策树
- ML学习笔记-决策树
- 【ML笔记】决策树剪枝
- 决策树-ML之三
- ML--决策树学习小结
- ML—决策树(train,matlab)
- 「ML」Chap4决策树
- ML基础---决策树的应用
- 如何优雅的ML(二) 决策树
- 【ML算法】决策树——ID3算法
- 【ML--09】决策树算法Decision Tree
- 决策树(Decision Tree)-机器学习ML
- OpenCV(4)ML库->Decision Tree决策树
- ML—决策树算法实现(train+test,matlab)
- spark厦大---决策树分类器 -- spark.ml
- 【ML笔记】决策树ID3和C4.5算法的异同
- 【ML学习笔记】9:认识Decision Tree决策树
- ml
- AsyncTask浅析
- vim配置收集
- vue.js 踩坑
- 【Linux】中的守护进程
- 人为什么而活
- ML-决策树
- Git 相关
- poj bfs/dfs求树直径相关之2361 Roads in the North
- mongod参数说明文档,非常实用。
- Libevent 官方文档学习笔记(2. bufferevent部分)
- dede采集或更新搜索文章时cpu100%跑满卡死解决方法
- BZOJ 3781 小B的询问 莫队
- Libevent 官方文档学习笔记(3. evbuffer部分)
- Linux vi vim 试用心得