機器學習算法 --- Decision Trees Algorithms


一、Decision Trees Agorithms的簡介

   決策樹算法(Decision Trees Agorithms),是如今最流行的機器學習算法之一,它即能做分類又做回歸(不像之前介紹的其他學習算法),在本文中,將介紹如何用它來對數據做分類。

  本文參照了Madhu Sanjeevi ( Mady )的Decision Trees Algorithms,有能力的讀者可去閱讀原文。

  說明:本文有幾處直接引用了原文,並不是不想做翻譯,而是感覺翻譯過來總感覺不夠清晰,而原文卻講的很明白清晰。(個人觀點:任何語言的翻譯都會損失一定量的信息,所以盡量支持原版)

二、Why Decision trees?

   在已經有了很多種學習算法的情況下,為什么還要創造出回歸樹這種學習算法呢?它相比於其他算法有和優點?

    至於為什么,原因有很多,這里主要講兩點,這兩點也是在我看來相比於其他算法最大的優點。

    其一,決策樹的算法思想與人類做決定時的思考方式很相似,它相比於其他算法,無需計算很多很多的各種參數,它能像人類一樣綜合各種考慮,做出很好的選擇(不一定是最好啊ㄟ(▔,▔)ㄏ)。

    其二,它能將它做出決策的邏輯過程可視化(不同於SVM, NN, 或是神經網絡等,對於用戶而言是一個黑盒), 例如下圖,就是一個銀行是否給客戶發放貸款使用決策樹決策的一個過程。

 

 

三、What is the decision tree??

  A decision tree is a tree where each node represents a feature(attribute), each link(branch) represents a decision(rule) and each leaf represents an outcome(categorical or continues value).

  類似於下圖中左邊的數據,對於數據的分類我們使用右邊的方式對其分類:

  step 1:判斷Age,Age<27.5,則Class=High;否則,執行step 2。

  step 2: 判斷CarType,CarType∈Sports,則Class=High;否則Class=Low。

  對於一組數據,只需按照決策樹的分支一步步的走下去,便可得到最終的結果,有點兒類似於程序設計中的多分支選擇結構。

 

四、How to build this??

  學習新知識,最主要的三個問題就是why,what,how。前兩個問題已經在上面的介紹中解決了,接下來就是how,即如何建立一顆決策樹?

  

  建立決策樹,有很多種算法,本文主要講解一下兩種:

  1. ID3 (Iterative Dichotomiser 3) → uses Entropy function and Information gain as metrics.
  2. CART (Classification and Regression Trees) → uses Gini Index(Classification) as metric.         

————————————————————————————————————————————————————————————————————————————————————————————————————— 首先,我們使用第一種算法來對一個經典的分類問題建立決策樹:

  

  Let’s just take a famous dataset in the machine learning world which is whether dataset(playing game Y or N based on whether condition).

  We have four X values (outlook,temp,humidity and windy) being categorical and one y value (play Y or N) also being categorical.

  So we need to learn the mapping (what machine learning always does) between X and y.

  This is a binary classification problem, lets build the tree using the ID3 algorithm.

  首先,決策樹,也是一棵樹,在計算機科學中,樹是一種數據結構,它有根節點(root node),分枝(branch),和葉子節點(leaf node)。

  而對於一顆決策樹,each node represents a feature(attribute),so first, we need to choose the root node from (outlook, temp, humidity, windy). 那么改如何選擇呢?

  Answer: Determine the attribute that best classifies the training data; use this attribute at the root of the tree. Repeat this process at for each branch. 

  這也就意味着,我們要對決策樹的空間進行自頂向下的貪婪搜索。

  所以問題又來了,how do we choose the best attribute? 

  Answer: use the attribute with the highest information gain in ID3.

  

  In order to define information gain precisely, we begin by defining a measure commonly used in information theory, called entropy(熵) that characterizes the impurity of an arbitrary collection of examples.”

  So what's the entropy? (下圖是wikipedia給出的定義)

  從上面的公式中我們可以得到,對於一個二分類問題,如果entropy=0,則要么全為正樣本,要么全為負樣本(即理論上樣本應該屬於兩個,實際上所有的樣本全屬於一類)。如果entropy=1,則正負樣本各占一半。

  有了Entropy的概念,便可以定義Information gain:

  有了上述兩個概念,便可建立決策樹了,步驟如下:          

1.compute the entropy for data-set 2.for every attribute/feature: 1.calculate entropy for all categorical values 2.take average information entropy for the current attribute 3.calculate gain for the current attribute 3. pick the highest gain attribute. 4. Repeat until we get the tree we desired.

 

  對於這個實例,我們來具體使用一下它:

    step1(計算數據集整體的entropy):

    step2(計算每一項feature的entropy and information gain):

      這里只計算了兩項,其他兩項的計算方法類似。

    step3 (選擇Info gain最高的屬性):

      

      上表列出了每一項feature的entropy and information gain,我們可以發現Outlook便是我們要找的那個attribute。

    So our root node is Outlook:

  

   接着對於圖中左邊的未知節點,我們將由sunny得來的數據當做數據集,然后從這些數據中按照上述的步驟選擇其他三個屬性的一種作為此節點,對於右邊的節點做類似操作即可:

  最終,建立的決策樹如下:

  

—————————————————————————————————————————————————————————————————————————————————————————————————————  接着,我們使用第二種算法來建立決策樹(Classification with using the CART algorithm):

    CART算法其實與ID3非常相像,只是每次選擇時的指標不同,在ID3中我們使用entropy來計算Informaition gain,而在CART中,我們使用Gini index來計算Gini gain。

    同樣的,對於一個二分類問題而言(Yes or No),有四種組合:1 0 , 0 1 , 1 0 , 0 0,則存在

P(Target=1).P(Target=1) + P(Target=1).P(Target=0) + P(Target=0).P(Target=1) + P(Target=0).P(Target=0) = 1

P(Target=1).P(Target=0) + P(Target=0).P(Target=1) = 1 — P^2(Target=0) — P^2(Target=1)

    那么,對於二分類問題的Gini index定義如下:

  A Gini score gives an idea of how good a split is by how mixed the classes are in the two groups created by the split. A perfect separation results in a Gini score of 0, whereas the worst case split that results in 50/50 classes.

   所以,對於一個二分類問題,最大的Gini index:

  = 1 — (1/2)^2 — (1/2)^2
  = 1–2*(1/2)^2
  = 1- 2*(1/4)
  = 1–0.5
  = 0.5

  和二分類類似,我們可以定義出多分類時Gini index的計算公式:

  

  Maximum value of Gini Index could be when all target values are equally distributed.

  同樣的,當取最大的Gini index時,可以寫為(一共有k類且每一類數量相等時): = 1–1/k

  當所有樣本屬於同一類別時,Gini index為0。

  此時我們就可以根據Gini gani來選擇所需的node,Gini gani的計算公式(類似於information gain的計算)如下:

  那么便可以使用類似於ID3的算法的思想建立decision tree,步驟如下:

1.compute the gini index for data-set 2.for every attribute/feature: 1.calculate gini index for all categorical values 2.take average information entropy(這里指GiniGain(A,S)的右半部分,跟ID3中的不同) for the current attribute 
3.calculate the gini gain 3. pick the best gini gain attribute. 4. Repeat until we get the tree we desired.

 

  最終,形成的decision tree如下:

  其實這兩種算法本質沒有任何區別,只是選擇node時所用的指標(表達式)不同而已。

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM