最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解讀python如何實(shí)現(xiàn)決策樹算法

 更新時(shí)間:2018年10月11日 10:49:07   投稿:laozhang  
在本篇文章里我們給讀者們分享了關(guān)于python如何實(shí)現(xiàn)決策樹算法的相關(guān)知識點(diǎn)內(nèi)容,需要的朋友們參考下。

數(shù)據(jù)描述

每條數(shù)據(jù)項(xiàng)儲存在列表中,最后一列儲存結(jié)果

多條數(shù)據(jù)項(xiàng)形成數(shù)據(jù)集

data=[[d1,d2,d3...dn,result],
   [d1,d2,d3...dn,result],
        .
        .
   [d1,d2,d3...dn,result]]

決策樹數(shù)據(jù)結(jié)構(gòu)

class DecisionNode:
  '''決策樹節(jié)點(diǎn)
  '''
   
  def __init__(self,col=-1,value=None,results=None,tb=None,fb=None):
    '''初始化決策樹節(jié)點(diǎn)
     
    args:    
    col -- 按數(shù)據(jù)集的col列劃分?jǐn)?shù)據(jù)集
    value -- 以value作為劃分col列的參照
    result -- 只有葉子節(jié)點(diǎn)有,代表最終劃分出的子數(shù)據(jù)集結(jié)果統(tǒng)計(jì)信息。{‘結(jié)果':結(jié)果出現(xiàn)次數(shù)}
    rb,fb -- 代表左右子樹
    '''
    self.col=col
    self.value=value
    self.results=results
    self.tb=tb
    self.fb=fb

決策樹分類的最終結(jié)果是將數(shù)據(jù)項(xiàng)劃分出了若干子集,其中每個(gè)子集的結(jié)果都一樣,所以這里采用{‘結(jié)果':結(jié)果出現(xiàn)次數(shù)}的方式表達(dá)每個(gè)子集

def pideset(rows,column,value):
  '''依據(jù)數(shù)據(jù)集rows的column列的值,判斷其與參考值value的關(guān)系對數(shù)據(jù)集進(jìn)行拆分
    返回兩個(gè)數(shù)據(jù)集
  '''
  split_function=None
  #value是數(shù)值類型
  if isinstance(value,int) or isinstance(value,float):
    #定義lambda函數(shù)當(dāng)row[column]>=value時(shí)返回true
    split_function=lambda row:row[column]>=value
  #value是字符類型
  else:
    #定義lambda函數(shù)當(dāng)row[column]==value時(shí)返回true
    split_function=lambda row:row[column]==value
  #將數(shù)據(jù)集拆分成兩個(gè)
  set1=[row for row in rows if split_function(row)]
  set2=[row for row in rows if not split_function(row)]
  #返回兩個(gè)數(shù)據(jù)集
  return (set1,set2)
 
def uniquecounts(rows):
  '''計(jì)算數(shù)據(jù)集rows中有幾種最終結(jié)果,計(jì)算結(jié)果出現(xiàn)次數(shù),返回一個(gè)字典
  '''
  results={}
  for row in rows:
    r=row[len(row)-1]
    if r not in results: results[r]=0
    results[r]+=1
  return results
 
def giniimpurity(rows):
  '''返回rows數(shù)據(jù)集的基尼不純度
  '''
  total=len(rows)
  counts=uniquecounts(rows)
  imp=0
  for k1 in counts:
    p1=float(counts[k1])/total
    for k2 in counts:
      if k1==k2: continue
      p2=float(counts[k2])/total
      imp+=p1*p2
  return imp
 
def entropy(rows):
  '''返回rows數(shù)據(jù)集的熵
  '''
  from math import log
  log2=lambda x:log(x)/log(2) 
  results=uniquecounts(rows)
  ent=0.0
  for r in results.keys():
    p=float(results[r])/len(rows)
    ent=ent-p*log2(p)
  return ent
 
def build_tree(rows,scoref=entropy):
  '''構(gòu)造決策樹
  '''
  if len(rows)==0: return DecisionNode()
  current_score=scoref(rows)
 
  # 最佳信息增益
  best_gain=0.0
  #
  best_criteria=None
  #最佳劃分
  best_sets=None
 
  column_count=len(rows[0])-1
  #遍歷數(shù)據(jù)集的列,確定分割順序
  for col in range(0,column_count):
    column_values={}
    # 構(gòu)造字典
    for row in rows:
      column_values[row[col]]=1
    for value in column_values.keys():
      (set1,set2)=pideset(rows,col,value)
      p=float(len(set1))/len(rows)
      # 計(jì)算信息增益
      gain=current_score-p*scoref(set1)-(1-p)*scoref(set2)
      if gain>best_gain and len(set1)>0 and len(set2)>0:
        best_gain=gain
        best_criteria=(col,value)
        best_sets=(set1,set2)
  # 如果劃分的兩個(gè)數(shù)據(jù)集熵小于原數(shù)據(jù)集,進(jìn)一步劃分它們
  if best_gain>0:
    trueBranch=build_tree(best_sets[0])
    falseBranch=build_tree(best_sets[1])
    return DecisionNode(col=best_criteria[0],value=best_criteria[1],
            tb=trueBranch,fb=falseBranch)
  # 如果劃分的兩個(gè)數(shù)據(jù)集熵不小于原數(shù)據(jù)集,停止劃分
  else:
    return DecisionNode(results=uniquecounts(rows))
 
def print_tree(tree,indent=''):
  if tree.results!=None:
    print(str(tree.results))
  else:
    print(str(tree.col)+':'+str(tree.value)+'? ')
    print(indent+'T->',end='')
    print_tree(tree.tb,indent+' ')
    print(indent+'F->',end='')
    print_tree(tree.fb,indent+' ')
 
 
def getwidth(tree):
  if tree.tb==None and tree.fb==None: return 1
  return getwidth(tree.tb)+getwidth(tree.fb)
 
def getdepth(tree):
  if tree.tb==None and tree.fb==None: return 0
  return max(getdepth(tree.tb),getdepth(tree.fb))+1
 
 
def drawtree(tree,jpeg='tree.jpg'):
  w=getwidth(tree)*100
  h=getdepth(tree)*100+120
 
  img=Image.new('RGB',(w,h),(255,255,255))
  draw=ImageDraw.Draw(img)
 
  drawnode(draw,tree,w/2,20)
  img.save(jpeg,'JPEG')
 
def drawnode(draw,tree,x,y):
  if tree.results==None:
    # Get the width of each branch
    w1=getwidth(tree.fb)*100
    w2=getwidth(tree.tb)*100
 
    # Determine the total space required by this node
    left=x-(w1+w2)/2
    right=x+(w1+w2)/2
 
    # Draw the condition string
    draw.text((x-20,y-10),str(tree.col)+':'+str(tree.value),(0,0,0))
 
    # Draw links to the branches
    draw.line((x,y,left+w1/2,y+100),fill=(255,0,0))
    draw.line((x,y,right-w2/2,y+100),fill=(255,0,0))
   
    # Draw the branch nodes
    drawnode(draw,tree.fb,left+w1/2,y+100)
    drawnode(draw,tree.tb,right-w2/2,y+100)
  else:
    txt=' \n'.join(['%s:%d'%v for v in tree.results.items()])
    draw.text((x-20,y),txt,(0,0,0))

對測試數(shù)據(jù)進(jìn)行分類(附帶處理缺失數(shù)據(jù))

def mdclassify(observation,tree):
  '''對缺失數(shù)據(jù)進(jìn)行分類
   
  args:
  observation -- 發(fā)生信息缺失的數(shù)據(jù)項(xiàng)
  tree -- 訓(xùn)練完成的決策樹
   
  返回代表該分類的結(jié)果字典
  '''
 
  # 判斷數(shù)據(jù)是否到達(dá)葉節(jié)點(diǎn)
  if tree.results!=None:
    # 已經(jīng)到達(dá)葉節(jié)點(diǎn),返回結(jié)果result
    return tree.results
  else:
    # 對數(shù)據(jù)項(xiàng)的col列進(jìn)行分析
    v=observation[tree.col]
 
    # 若col列數(shù)據(jù)缺失
    if v==None:
      #對tree的左右子樹分別使用mdclassify,tr是左子樹得到的結(jié)果字典,fr是右子樹得到的結(jié)果字典
      tr,fr=mdclassify(observation,tree.tb),mdclassify(observation,tree.fb)
 
      # 分別以結(jié)果占總數(shù)比例計(jì)算得到左右子樹的權(quán)重
      tcount=sum(tr.values())
      fcount=sum(fr.values())
      tw=float(tcount)/(tcount+fcount)
      fw=float(fcount)/(tcount+fcount)
      result={}
 
      # 計(jì)算左右子樹的加權(quán)平均
      for k,v in tr.items(): 
        result[k]=v*tw
      for k,v in fr.items(): 
        # fr的結(jié)果k有可能并不在tr中,在result中初始化k
        if k not in result: 
          result[k]=0 
        # fr的結(jié)果累加到result中 
        result[k]+=v*fw
      return result
 
    # col列沒有缺失,繼續(xù)沿決策樹分類
    else:
      if isinstance(v,int) or isinstance(v,float):
        if v>=tree.value: branch=tree.tb
        else: branch=tree.fb
      else:
        if v==tree.value: branch=tree.tb
        else: branch=tree.fb
      return mdclassify(observation,branch)
 
tree=build_tree(my_data)
print(mdclassify(['google',None,'yes',None],tree))
print(mdclassify(['google','France',None,None],tree))

決策樹剪枝

def prune(tree,mingain):
  '''對決策樹進(jìn)行剪枝
   
  args:
  tree -- 決策樹
  mingain -- 最小信息增益
   
  返回
  '''
  # 修剪非葉節(jié)點(diǎn)
  if tree.tb.results==None:
    prune(tree.tb,mingain)
  if tree.fb.results==None:
    prune(tree.fb,mingain)
  #合并兩個(gè)葉子節(jié)點(diǎn)
  if tree.tb.results!=None and tree.fb.results!=None:
    tb,fb=[],[]
    for v,c in tree.tb.results.items():
      tb+=[[v]]*c
    for v,c in tree.fb.results.items():
      fb+=[[v]]*c
    #計(jì)算熵減少情況
    delta=entropy(tb+fb)-(entropy(tb)+entropy(fb)/2)
    #熵的增加量小于mingain,可以合并分支
    if delta<mingain:
      tree.tb,tree.fb=None,None
      tree.results=uniquecounts(tb+fb)

相關(guān)文章

  • Django分組聚合查詢實(shí)例分享

    Django分組聚合查詢實(shí)例分享

    在本篇文章里小編給大家分享的是關(guān)于Django分組聚合查詢實(shí)例內(nèi)容,需要的朋友們可以參考下。
    2020-04-04
  • Python實(shí)現(xiàn)修改IE注冊表功能示例

    Python實(shí)現(xiàn)修改IE注冊表功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)修改IE注冊表功能,結(jié)合完整實(shí)例形式分析了Python操作IE注冊表項(xiàng)的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-05-05
  • 利用Python實(shí)現(xiàn)kNN算法的代碼

    利用Python實(shí)現(xiàn)kNN算法的代碼

    這篇文章主要介紹了利用Python實(shí)現(xiàn)kNN算法的代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 解決每次打開pycharm直接進(jìn)入項(xiàng)目的問題

    解決每次打開pycharm直接進(jìn)入項(xiàng)目的問題

    今天小編就為大家分享一篇解決每次打開pycharm直接進(jìn)入項(xiàng)目的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python中安裝Scrapy模塊依賴包匯總

    python中安裝Scrapy模塊依賴包匯總

    Scrapy的安裝有一些依賴包,沒有這些包是會安裝失敗的,下面我們就來詳細(xì)探討下
    2017-07-07
  • Pandas如何對帶有Multi-column(多列名稱)的數(shù)據(jù)排序并寫入Excel中

    Pandas如何對帶有Multi-column(多列名稱)的數(shù)據(jù)排序并寫入Excel中

    這篇文章主要介紹了Pandas如何對帶有Multi-column(多列名稱)的數(shù)據(jù)排序并寫入Excel中問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python 保持登錄狀態(tài)進(jìn)行接口測試的方法示例

    Python 保持登錄狀態(tài)進(jìn)行接口測試的方法示例

    這篇文章主要介紹了Python 保持登錄狀態(tài)進(jìn)行接口測試的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-08-08
  • Pytorch mask_select 函數(shù)的用法詳解

    Pytorch mask_select 函數(shù)的用法詳解

    今天小編就為大家分享一篇Pytorch mask_select 函數(shù)的用法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 利用Python編寫個(gè)有趣的記仇本

    利用Python編寫個(gè)有趣的記仇本

    這篇文章主要為大家介紹了一個(gè)有趣的小案例——利用Python編寫個(gè)有趣的記仇本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下
    2023-04-04
  • Python urlopen()函數(shù) 示例分享

    Python urlopen()函數(shù) 示例分享

    urlopen(url, data=None, proxies=None) 即創(chuàng)建一個(gè)表示遠(yuǎn)程url的類文件對象,然后像本地文件一樣操作這個(gè)類文件對象來獲取遠(yuǎn)程數(shù)據(jù)。參數(shù)url表示遠(yuǎn)程數(shù)據(jù)的路徑,一般是網(wǎng)址;參數(shù)data表示以post方式提交到url的數(shù)據(jù);參數(shù)proxies用于設(shè)置代理。
    2014-06-06

最新評論

石阡县| 陵川县| 时尚| 泰顺县| 通道| 江永县| 邹平县| 华坪县| 蒲城县| 微山县| 凌源市| 普安县| 瑞昌市| 岳阳市| 宜兰市| 汝南县| 东港市| 报价| 隆回县| 玉田县| 阿图什市| 清涧县| 佛山市| 南京市| 曲周县| 西宁市| 克东县| 象州县| 西吉县| 新乡市| 云龙县| 邮箱| 阿拉善左旗| 桓仁| 哈尔滨市| 龙州县| 长垣县| 新蔡县| 盐边县| 凯里市| 渑池县|