侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

基于ID3决策树算法的实现(Python版)

Python  /  管理员 发布于 7年前   235

实例如下:

# -*- coding:utf-8 -*-from numpy import *import numpy as npimport pandas as pdfrom math import logimport operator#计算数据集的香农熵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  #以2为底数计算香农熵  for key in labelCounts:    prob = float(labelCounts[key])/numEntries    shannonEnt-=prob*log(prob,2)  return shannonEnt#对离散变量划分数据集,取出该特征取值为value的所有样本def splitDataSet(dataSet,axis,value):  retDataSet=[]  for featVec in dataSet:    if featVec[axis]==value:      reducedFeatVec=featVec[:axis]      reducedFeatVec.extend(featVec[axis+1:])      retDataSet.append(reducedFeatVec)  return retDataSet#对连续变量划分数据集,direction规定划分的方向,#决定是划分出小于value的数据样本还是大于value的数据样本集def splitContinuousDataSet(dataSet,axis,value,direction):  retDataSet=[]  for featVec in dataSet:    if direction==0:      if featVec[axis]>value:        reducedFeatVec=featVec[:axis]        reducedFeatVec.extend(featVec[axis+1:])        retDataSet.append(reducedFeatVec)    else:      if featVec[axis]<=value:        reducedFeatVec=featVec[:axis]        reducedFeatVec.extend(featVec[axis+1:])        retDataSet.append(reducedFeatVec)  return retDataSet#选择最好的数据集划分方式def chooseBestFeatureToSplit(dataSet,labels):  numFeatures=len(dataSet[0])-1  baseEntropy=calcShannonEnt(dataSet)  bestInfoGain=0.0  bestFeature=-1  bestSplitDict={}  for i in range(numFeatures):    featList=[example[i] for example in dataSet]    #对连续型特征进行处理    if type(featList[0]).__name__=='float' or type(featList[0]).__name__=='int':      #产生n-1个候选划分点      sortfeatList=sorted(featList)      splitList=[]      for j in range(len(sortfeatList)-1):        splitList.append((sortfeatList[j]+sortfeatList[j+1])/2.0)      bestSplitEntropy=10000      slen=len(splitList)      #求用第j个候选划分点划分时,得到的信息熵,并记录最佳划分点      for j in range(slen):        value=splitList[j]        newEntropy=0.0        subDataSet0=splitContinuousDataSet(dataSet,i,value,0)        subDataSet1=splitContinuousDataSet(dataSet,i,value,1)        prob0=len(subDataSet0)/float(len(dataSet))        newEntropy+=prob0*calcShannonEnt(subDataSet0)        prob1=len(subDataSet1)/float(len(dataSet))        newEntropy+=prob1*calcShannonEnt(subDataSet1)        if newEntropy<bestSplitEntropy:          bestSplitEntropy=newEntropy          bestSplit=j      #用字典记录当前特征的最佳划分点      bestSplitDict[labels[i]]=splitList[bestSplit]      infoGain=baseEntropy-bestSplitEntropy    #对离散型特征进行处理    else:      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  #若当前节点的最佳划分特征为连续特征,则将其以之前记录的划分点为界进行二值化处理  #即是否小于等于bestSplitValue  if type(dataSet[0][bestFeature]).__name__=='float' or type(dataSet[0][bestFeature]).__name__=='int':    bestSplitValue=bestSplitDict[labels[bestFeature]]    labels[bestFeature]=labels[bestFeature]+'<='+str(bestSplitValue)    for i in range(shape(dataSet)[0]):      if dataSet[i][bestFeature]<=bestSplitValue:        dataSet[i][bestFeature]=1      else:        dataSet[i][bestFeature]=0  return bestFeature#特征若已经划分完,节点下的样本还没有统一取值,则需要进行投票def majorityCnt(classList):  classCount={}  for vote in classList:    if vote not in classCount.keys():      classCount[vote]=0    classCount[vote]+=1  return max(classCount)#主程序,递归产生决策树def createTree(dataSet,labels,data_full,labels_full):  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,labels)  bestFeatLabel=labels[bestFeat]  myTree={bestFeatLabel:{}}  featValues=[example[bestFeat] for example in dataSet]  uniqueVals=set(featValues)  if type(dataSet[0][bestFeat]).__name__=='str':    currentlabel=labels_full.index(labels[bestFeat])    featValuesFull=[example[currentlabel] for example in data_full]    uniqueValsFull=set(featValuesFull)  del(labels[bestFeat])  #针对bestFeat的每个取值,划分出一个子树。  for value in uniqueVals:    subLabels=labels[:]    if type(dataSet[0][bestFeat]).__name__=='str':      uniqueValsFull.remove(value)    myTree[bestFeatLabel][value]=createTree(splitDataSet\     (dataSet,bestFeat,value),subLabels,data_full,labels_full)  if type(dataSet[0][bestFeat]).__name__=='str':    for value in uniqueValsFull:      myTree[bestFeatLabel][value]=majorityCnt(classList)  return myTreeimport matplotlib.pyplot as pltdecisionNode=dict(boxstyle="sawtooth",fc="0.8")leafNode=dict(boxstyle="round4",fc="0.8")arrow_args=dict(arrowstyle="<-")#计算树的叶子节点数量def getNumLeafs(myTree):  numLeafs=0  firstSides = list(myTree.keys())  firstStr=firstSides[0]  secondDict=myTree[firstStr]  for key in secondDict.keys():    if type(secondDict[key]).__name__=='dict':      numLeafs+=getNumLeafs(secondDict[key])    else: numLeafs+=1  return numLeafs#计算树的最大深度def getTreeDepth(myTree):  maxDepth=0  firstSides = list(myTree.keys())  firstStr=firstSides[0]  secondDict=myTree[firstStr]  for key in secondDict.keys():    if type(secondDict[key]).__name__=='dict':      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):  lens=len(txtString)  xMid=(parentPt[0]+cntrPt[0])/2.0-lens*0.002  yMid=(parentPt[1]+cntrPt[1])/2.0  createPlot.ax1.text(xMid,yMid,txtString)def plotTree(myTree,parentPt,nodeTxt):  numLeafs=getNumLeafs(myTree)  depth=getTreeDepth(myTree)  firstSides = list(myTree.keys())  firstStr=firstSides[0]  cntrPt=(plotTree.x0ff+(1.0+float(numLeafs))/2.0/plotTree.totalW,plotTree.y0ff)  plotMidText(cntrPt,parentPt,nodeTxt)  plotNode(firstStr,cntrPt,parentPt,decisionNode)  secondDict=myTree[firstStr]  plotTree.y0ff=plotTree.y0ff-1.0/plotTree.totalD  for key in secondDict.keys():    if type(secondDict[key]).__name__=='dict':      plotTree(secondDict[key],cntrPt,str(key))    else:      plotTree.x0ff=plotTree.x0ff+1.0/plotTree.totalW      plotNode(secondDict[key],(plotTree.x0ff,plotTree.y0ff),cntrPt,leafNode)      plotMidText((plotTree.x0ff,plotTree.y0ff),cntrPt,str(key))  plotTree.y0ff=plotTree.y0ff+1.0/plotTree.totalDdef createPlot(inTree):  fig=plt.figure(1,facecolor='white')  fig.clf()  axprops=dict(xticks=[],yticks=[])  createPlot.ax1=plt.subplot(111,frameon=False,**axprops)  plotTree.totalW=float(getNumLeafs(inTree))  plotTree.totalD=float(getTreeDepth(inTree))  plotTree.x0ff=-0.5/plotTree.totalW  plotTree.y0ff=1.0  plotTree(inTree,(0.5,1.0),'')  plt.show()df=pd.read_csv('watermelon_4_3.csv')data=df.values[:,1:].tolist()data_full=data[:]labels=df.columns.values[1:-1].tolist()labels_full=labels[:]myTree=createTree(data,labels,data_full,labels_full)print(myTree)createPlot(myTree)

最终结果如下:

{'texture': {'blur': 0, 'little_blur': {'touch': {'soft_stick': 1, 'hard_smooth': 0}}, 'distinct': {'density<=0.38149999999999995': {0: 1, 1: 0}}}}

得到的决策树如下:

参考资料:

《机器学习实战》

《机器学习》周志华著

以上这篇基于ID3决策树算法的实现(Python版)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


  • 上一条:
    python实现决策树C4.5算法详解(在ID3基础上改进)
    下一条:
    Python基础知识_浅谈用户交互
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客