Reinventing the wheel:決策樹演算法的實現

木樹發表於2019-02-16

資料描述

每條資料項儲存在列表中,最後一列儲存結果
多條資料項形成資料集

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

決策樹資料結構

class DecisionNode:
    ```決策樹節點
    ```
    
    def __init__(self,col=-1,value=None,results=None,tb=None,fb=None):
        ```初始化決策樹節點
        
        args:        
        col -- 按資料集的col列劃分資料集
        value -- 以value作為劃分col列的參照
        result -- 只有葉子節點有,代表最終劃分出的子資料集結果統計資訊。{‘結果’:結果出現次數}
        rb,fb -- 代表左右子樹
        ```
        self.col=col
        self.value=value
        self.results=results
        self.tb=tb
        self.fb=fb

決策樹分類的最終結果是將資料項劃分出了若干子集,其中每個子集的結果都一樣,所以這裡採用{‘結果’:結果出現次數}的方式表達每個子集


def divideset(rows,column,value):
    ```依據資料集rows的column列的值,判斷其與參考值value的關係對資料集進行拆分
       返回兩個資料集
    ```
    split_function=None
    #value是數值型別
    if isinstance(value,int) or isinstance(value,float):
        #定義lambda函式當row[column]>=value時返回true
        split_function=lambda row:row[column]>=value
    #value是字元型別
    else:
        #定義lambda函式當row[column]==value時返回true
        split_function=lambda row:row[column]==value
    #將資料集拆分成兩個
    set1=[row for row in rows if split_function(row)]
    set2=[row for row in rows if not split_function(row)]
    #返回兩個資料集
    return (set1,set2)

def uniquecounts(rows):
    ```計算資料集rows中有幾種最終結果,計算結果出現次數,返回一個字典
    ```
    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資料集的基尼不純度
    ```
    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資料集的熵
    ```
    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):
    ```構造決策樹
    ```
    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
    #遍歷資料集的列,確定分割順序
    for col in range(0,column_count):
        column_values={}
        # 構造字典
        for row in rows:
            column_values[row[col]]=1
        for value in column_values.keys():
            (set1,set2)=divideset(rows,col,value)
            p=float(len(set1))/len(rows)
            # 計算資訊增益
            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)
    # 如果劃分的兩個資料集熵小於原資料集,進一步劃分它們
    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)
    # 如果劃分的兩個資料集熵不小於原資料集,停止劃分
    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=` 
`.join([`%s:%d`%v for v in tree.results.items()])
        draw.text((x-20,y),txt,(0,0,0))



對測試資料進行分類(附帶處理缺失資料)

def mdclassify(observation,tree):
    ```對缺失資料進行分類
    
    args:
    observation -- 發生資訊缺失的資料項
    tree -- 訓練完成的決策樹
    
    返回代表該分類的結果字典
    ```

    # 判斷資料是否到達葉節點
    if tree.results!=None:
        # 已經到達葉節點,返回結果result
        return tree.results
    else:
        # 對資料項的col列進行分析
        v=observation[tree.col]

        # 若col列資料缺失
        if v==None:
            #對tree的左右子樹分別使用mdclassify,tr是左子樹得到的結果字典,fr是右子樹得到的結果字典
            tr,fr=mdclassify(observation,tree.tb),mdclassify(observation,tree.fb)

            # 分別以結果佔總數比例計算得到左右子樹的權重
            tcount=sum(tr.values())
            fcount=sum(fr.values())
            tw=float(tcount)/(tcount+fcount)
            fw=float(fcount)/(tcount+fcount)
            result={}

            # 計算左右子樹的加權平均
            for k,v in tr.items(): 
                result[k]=v*tw
            for k,v in fr.items(): 
                # fr的結果k有可能並不在tr中,在result中初始化k
                if k not in result: 
                    result[k]=0 
                # fr的結果累加到result中  
                result[k]+=v*fw
            return result

        # col列沒有缺失,繼續沿決策樹分類
        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):
    ```對決策樹進行剪枝
    
    args:
    tree -- 決策樹
    mingain -- 最小資訊增益
    
   返回
    ```
    # 修剪非葉節點
    if tree.tb.results==None:
        prune(tree.tb,mingain)
    if tree.fb.results==None:
        prune(tree.fb,mingain)
    #合併兩個葉子節點
    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
        #計算熵減少情況
        delta=entropy(tb+fb)-(entropy(tb)+entropy(fb)/2)
        #熵的增加量小於mingain,可以合併分支
        if delta<mingain:
            tree.tb,tree.fb=None,None
            tree.results=uniquecounts(tb+fb)

相關文章