# Decision Tree

## Decision Tree

* 데이터를 분석하여 이들 사이에 존재하는 패턴을 예측 가능한 규칙들의 조합으로 나타냄.
* 분류(Classification)와 회귀(Regression) 문제에 적용 가능.
* 계산복잡성 대비 높은 예측 성능을 지님.

  <img src="https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-7f7cfa11f8a9a657ab9c69333f81679fdaab8c47%2Fdecision_tree.png?alt=media" alt="" data-size="original">

### Example (Process of Decsion Tree)

* Find the feature and criteria that best differentiate the data.
* Q) How to findd the criteria that best differentiate the data?

  * -> Maximize the **information gain**. (Minimize the **Impurity (Entropy)**)

  ![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-459e8478a606ce3650deaa59d7b2a46e96c2d7fe%2Fex1.png?alt=media) ![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-995bb26f5159c2ff69c77990cc776c9a0511aea7%2Fex2.png?alt=media) ![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-36ced8ebb8a60df6616a2b4a2f2d6eea756e4547%2Fex3.png?alt=media)

### Entropy, Impurity

* 불순도 (Impruty)
  * 해당 범주 안에 서로 다른 데이터가 얼마나 섞여 있는가?
* Entropy
  * 불순도를 수치적으로 나타낸 척도

$$Entropy = - \sum\nolimits\_i {({p\_i}){{\log }\_2}({p\_i})}$$

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-07aabd9f57d9461d219902f4fc9382fa2119c654%2Fimpurity.png?alt=media)

#### Entropy Example

![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-70136a8b2fd7f711d02cca005fd0b5086d8cf127%2Fentropy_example.png?alt=media)

### Impurity Indicator

* Entropy $$Entropy(A) = \sum\limits\_{i = 1}^d {{R\_i}\left( { - \sum\limits\_{k = 1}^m {{p\_k}{{\log }\_2}({p\_k})} } \right)}$$
* Gini-Index $$G.I(A) = \sum\limits\_{i = 1}^d {\left( {{R\_i}\left( {1 - \sum\limits\_{k = 1}^m {{p\_{ik}}^2} } \right)} \right)}$$

  <img src="https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-d69c2805bf565e9018d64759f0e42c6c21172ccd%2Fimpurity_indicator.png?alt=media" alt="" data-size="original">

### Rish of Overfitting

* Decision trees tend to overfit on data with a large number of features. Getting the right ratio of samples to number of features is important, since a tree with few samples in high dimensional space is very likely to overfit.
* We have to limit **the maximum depth, the maximum number of nodes, minimum number of data for one node to split**.

  <img src="https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-68f1816f820d36e8b209c768f7d430345afc11ce%2Foverfitting.png?alt=media" alt="" data-size="original">

### Pruning

* Prevent overfitting using pruning. ![](https://3698175758-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MAwtzMy_pbrChIExFtN%2Fuploads%2Fgit-blob-6d527c4f0ff8191c0a01f6c81e5342cfb21a5606%2Fpruning.png?alt=media)

#### Cost Function of Pruning

* 𝐶𝐶(𝑇)=𝐸𝑟𝑟(𝑇)+𝛼×𝐿(𝑇)
  * 𝐶𝐶(𝑇): 의사결정나무의 비용 복잡도 (=오류가 적으면서 terminal node 수가 적은 단순한 모델일수록 작은 값)
  * 𝐸𝑅𝑅(𝑇): 검증 데이터에 대한 오분류율
  * 𝐿(𝑇): terminal node의 수 (구조 복잡도)
  * 𝐴𝑙𝑝ℎ𝑎: 𝐸𝑅𝑅(𝑇)와 𝐿(𝑇)를 결합하는 가중치 (사용자에 의해 부여, 보통 0.01\~0.1의 값을 사용)

### Summary

* 의사결정나무는 한번에 하나씩의 설명변수를 사용하여 예측 가능한 규칙들의 집합을 생성하는 알고리즘.
  * 한번 분기 때마다 변수 영역을 두개로 구분한 뒤 각 영역의 순도가 증가(엔트로피가 감소)하는 방향으로 학습을 진행함.
  * 입력 변수 영역을 두 개로 구분하는 \*\*재귀적 분기(recursive partioning)\*\*와 구분된 영역을 통합하는 \*\*가지치기(pruning)\*\*의 두가지 과정으로 나뉨.
* 의사결정나무는 계산복잡성 대비 높은 예측 성능을 내는 장점을 지님.
* 하지만, 결정경계(decision boundary)가 데이터 축에 수직이어서 특정 데이터에만 잘 작동하는 한계가 있음.
  * 이러한 문제를 극복하기 위해 **랜덤포레스트**와 같은 기법을 사용.
  * 랜덤포레스트란?
    * 같은 데이터에 대해 의사결정나무를 여러 개 만들어서 그 결과를 조합해서 예측 성능을 높이는 기법
    * 동일한 데이터로부터 복원추출을 통해 30개 이상의 데이터셋을 만들어서 각각에 의사결정나무를 적욯안 뒤 학습 결과를 취합하는 방식으로 동작

## References

* Theoretical Explanation
  * <https://bkshin.tistory.com/entry/%EB%A8%B8%EC%8B%A0%EB%9F%AC%EB%8B%9D-4-%EA%B2%B0%EC%A0%95-%ED%8A%B8%EB%A6%ACDecision-Tree>
  * <https://ratsgo.github.io/machine%20learning/2017/03/26/tree/>
* Python Implementation of Decision Tree
  * <https://towardsdatascience.com/construct-a-decision-tree-and-how-to-deal-with-overfitting-f907efc1492d>
* Scikit-learn documentation
  * <https://scikit-learn.org/stable/modules/tree.html>
* MATHLAB Example
  * fitctree & fitrtree (instruction & algorithm)
    * <https://kr.mathworks.com/help/stats/fitctree.html>
    * <https://kr.mathworks.com/help/stats/fitrtree.html>
  * View Decision Tree
    * <https://kr.mathworks.com/help/stats/view-decision-tree.html>
  * Prediction Using Classification and Regression Trees
    * <https://kr.mathworks.com/help/stats/prediction-using-classification-and-regression-trees.html>
