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

1、Decision Trees Agorithms的簡介

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

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

  說明:本文有幾處直接引用了原文,並非不想作翻譯,而是感受翻譯過來總感受不夠清晰,而原文卻講的很明白清晰。(我的觀點:任何語言的翻譯都會損失必定量的信息,因此儘可能支持原版)網絡

2、Why Decision trees?

   在已經有了不少種學習算法的狀況下,爲何還要創造出迴歸樹這種學習算法呢?它相比於其餘算法有和優勢?數據結構

    至於爲何,緣由有不少,這裏主要講兩點,這兩點也是在我看來相比於其餘算法最大的優勢。app

    其一,決策樹的算法思想與人類作決定時的思考方式很類似,它相比於其餘算法,無需計算不少不少的各類參數,它能像人類同樣綜合各類考慮,作出很好的選擇(不必定是最好啊ㄟ(▔,▔)ㄏ)。機器學習

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

 

 

3、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).post

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

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

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

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

 

4、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時所用的指標(表達式)不一樣而已。

相關文章
相關標籤/搜索