决策树算法(Decision Trees Agorithms),是现在最流行的机器学习算法之一,它即能作分类又作回归(不像以前介绍的其余学习算法),在本文中,将介绍如何用它来对数据作分类。node
本文参照了Madhu Sanjeevi ( Mady )的Decision Trees Algorithms,有能力的读者可去阅读原文。算法
说明:本文有几处直接引用了原文,并非不想作翻译,而是感受翻译过来总感受不够清晰,而原文却讲的很明白清晰。(我的观点:任何语言的翻译都会损失必定量的信息,因此尽可能支持原版)网络
在已经有了不少种学习算法的状况下,为何还要创造出回归树这种学习算法呢?它相比于其余算法有和优势?数据结构
至于为何,缘由有不少,这里主要讲两点,这两点也是在我看来相比于其余算法最大的优势。app
其一,决策树的算法思想与人类作决定时的思考方式很类似,它相比于其余算法,无需计算不少不少的各类参数,它能像人类同样综合各类考虑,作出很好的选择(不必定是最好啊ㄟ(▔,▔)ㄏ)。机器学习
其二,它能将它作出决策的逻辑过程可视化(不一样于SVM, NN, 或是神经网络等,对于用户而言是一个黑盒), 例以下图,就是一个银行是否给客户发放贷款使用决策树决策的一个过程。ide
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。
对于一组数据,只需按照决策树的分支一步步的走下去,即可获得最终的结果,有点儿相似于程序设计中的多分支选择结构。
学习新知识,最主要的三个问题就是why,what,how。前两个问题已经在上面的介绍中解决了,接下来就是how,即如何创建一颗决策树?
创建决策树,有不少种算法,本文主要讲解一下两种:
————————————————————————————————————————————————————————————————————————————————————————————————————— 首先,咱们使用第一种算法来对一个经典的分类问题创建决策树:
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时所用的指标(表达式)不一样而已。