import numpy as np
import operator 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 calcShannonEnt(dataSet):
numEntries = len(dataSet) #计算机数据有多少行 labelCounts = {} #创建一个空字典
for featVec in dataSet:
currentLabel = featVec[-1] if currentLabel not in labelCounts.keys():
labelCounts[currentLabel]=0
labelCounts[currentLabel]+=1 shannonEnt = 0.0
for key in labelCounts:
prob = labelCounts[key]/numEntries
shannonEnt = shannonEnt - prob*np.log2(prob)
return shannonEnt def splitDataSet(dataSet,axis,value):
retDataSet = []
for featVec in dataSet:
if featVec[axis]==value:
reduceFeatVec = featVec[:axis]
reduceFeatVec.extend(featVec[axis+1:])
retDataSet.append(reduceFeatVec)
return retDataSet # 循环计算香农熵和splitDataSet()函数,找到最好的特征划分方式
def chooseBestFeatureToSplit(dataSet):
numFeatures = len(dataSet[0])-1
baseEntropy = calcShannonEnt(dataSet)
bestInfoGain = 0.0
bestFeature = -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
bestFeature = i
return bestFeature def majorityCnt(classList):
classCount = {}
for vote in classList:
if vote not in classCount.keys():
classCount = 0
classCount[vote] += 1
sortedClassCount = sorted(classCount.iteritems(), key=operator.itemgetter(1), reverse=True)
return sortedClassCount[0][0] # 参数:数据集和标签列表
def createTree(dataSet,labels):
classList = [example[-1] for example in dataSet] # 包含了数据集的所有类标签
if classList.count(classList[0]) == len(classList): # 当所有的类都相等时停止分裂
return classList[0]
if len(dataSet[0]) == 1: # 使用完了所有特征,仍然不能将数据集划分成仅包含唯一类别的分组
return majorityCnt(classList) # 采用多数多数原则选出分组
bestFeat = chooseBestFeatureToSplit(dataSet) # 选出最佳的特征值
bestFeatLabel = labels[bestFeat] # 获取该特征的名称 # 这里直接使用字典变量来存储树信息,这对于绘制树形图很重要。
myTree = {bestFeatLabel:{}} # 当前数据集选取最好的特征存储在bestFeat中
del(labels[bestFeat]) # 删除已经在选取的特征
featValues = [example[bestFeat] for example in dataSet]
uniqueVlas = set(featValues)
for value in uniqueVlas:
subLabels = labels[:] # 复制所有标签,这样树就不会破坏现有的标签
myTree[bestFeatLabel][value] = createTree(splitDataSet(dataSet, bestFeat, value),subLabels)
return myTree
myDat,labels = createDataSet()
print("myDat:",myDat) print(len(myDat[0]) -1 )
c = chooseBestFeatureToSplit(myDat) MyTree = createTree(myDat,labels)
print(MyTree)

最新文章

  1. 递归一题总结(OJ P1117倒牛奶)
  2. Centering, Scaling and Normalizing
  3. 启动hbase时出现HMaster Aborted错误
  4. PHP程序中删除字符串最后一个字符的三种方法
  5. c语言编程之二叉排序树
  6. LoadAssetAtPath 与 Load 的区别
  7. RPC框架Thrift例子-PHP调用C++后端程序
  8. IntelliJ 直接编辑国际化文件(properties)方法
  9. Use “error_messages” in Rails 3.2? (raises “undefined method” error)
  10. 关于bootstrap的一些想法
  11. 分享一个JS的Base64加密解密功能
  12. DHTMLX 常用技术
  13. 【BUAA-OO】第一单元作业总结
  14. 重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  15. CentOS7部署Flask+Gunicorn+Nginx+Supervisor
  16. Hadoop学习笔记(一):安装与配置
  17. django重定向
  18. [工具/PC]计算机中丢失libiconv-2.dll,丢失libintl-8.dll,无法定位程序输入点libiconv于动态链接库libiconv-2.dll上问题解决方法
  19. Redis went away
  20. soapui-groovy脚本中文乱码及符号乱码、响应乱码解决方案

热门文章

  1. 一 Mybatis概述&与Hibernate的区别&CRUD
  2. HTML学习第七天(二)
  3. PHP开发环境(Apache+mysql+PHPstorm+php)的搭建
  4. Python+opencv+pyaudio实现带声音屏幕录制
  5. 有没有比NRF51822更好的智能穿戴蓝牙方案
  6. PHP时间格式
  7. POJ 1195:Mobile phones 二维树状数组
  8. 【pwnable.kr】 memcpy
  9. 关于div水平垂直居中的几种方法
  10. js ajax跨域调用