
이 문서는
Feature Importance를
Decision Tree에서 Gini Impurity Measure를 이용하여 계산하는 예제를 보여줌.
Tree 예시 (depth = 3)
[Root]
(X1)
[5:5]
/ \
Node1 Node2
(X2) (X3)
[4:1] [1:4]
/ \ / \
Leaf1 Leaf2 Leaf3 Leaf4
[3:0] [1:1] [0:2] [1:2]
- Root 노드는
X1을 사용해 데이터를 분할. - Node1은
X2를 사용해 데이터를 다시 분할. - Node2는
X3을 사용해 데이터를 다시 분할.
단계 1: Gini impurity measure 계산
Gini impurity는 다음과 같이 계산:
$$
\text{Gini} = 1 - \sum_{i=1}^{n} (p_i^2)
$$
- $n$ : number of classes
- $p_i$ :현재 노드에서 인스턴스가 클래스 $i$에 속할 확률.
1-1. 초기 상태 (Root 노드):
- 클래스 0: 5개 샘플
- 클래스 1: 5개 샘플
지니 불순도 (Root):
$$1 - (0.5^2 + 0.5^2) = 0.5$$
단계 2: 각 노드에서의 분할 후 Gini impurity계산 및 중요도 평가
2-1. Root 노드에서 X1으로 분할
X1에 의해 분할된 두 그룹의 Gini impurity:
- 왼쪽 그룹 (Node1): 클래스 0 - 4개, 클래스 1 - 1개
$$1 - (0.8^2 + 0.2^2) = 0.32$$
- 오른쪽 그룹 (Node2): 클래스 0 - 1개, 클래스 1 - 4개
$$1 - (0.2^2 + 0.8^2) = 0.32$$
분할 전 Gini impurity는 0.5 이며, 이 노드에서의 분할 후 평균 Gini impurity measures는 다음과 같음:
$$
\frac{5}{10} \times 0.32 + \frac{5}{10} \times 0.32 = 0.32
$$
즉, Root 노드에서 Gini impurity decrement:
$$0.5 - 0.32 = 0.18$$
X1의 중요도에 기여: 0.18
2-2. Node1에서 X2로 분할
X2에 의해 분할된 두 그룹의 Gini impurity:
- 왼쪽 그룹 (Leaf1): 클래스 0 - 3개, 클래스 1 - 0개
$$1 - (1^2 + 0^2) = 0.0$$
- 오른쪽 그룹 (Leaf2): 클래스 0 - 1개, 클래스 1 - 1개
$$1 - (0.5^2 + 0.5^2) = 0.5$$
분할 전 inpurity는 0.32 이고 분할 후 평균 Gini impurity는 다음과 같음:
$$\frac{3}{5} \times 0.0 + \frac{2}{5} \times 0.5 = 0.2$$
Node1에서 Gini impurity decrement:
$$0.32 - 0.2 = 0.12$$
X2의 중요도에 기여: 0.12
2-3.Node2에서 X3으로 분할
X3에 의해 분할된 두 그룹의 Gini impurity:
- 왼쪽 그룹 (Leaf3): 클래스 0 - 0개, 클래스 1 - 2개
$$1 - (0^2 + 1^2) = 0.0$$
- 오른쪽 그룹 (Leaf4): 클래스 0 - 1개, 클래스 1 - 2개
$$1 - (1/3)^2 - (2/3)^2 = 0.44$$
분할 전 Gini impurity는 0.32 이고 분할 후 평균 Gini impurity는 다음과 같음:
$$\frac{2}{5} \times 0.0 + \frac{3}{5} \times 0.44 = 0.264$$
Node2에서 Gini impurity decrement:
$$0.32 - 0.264 = 0.056$$
X3의 중요도에 기여: 0.056
단계 3: 특성 중요도 계산 및 정규화
X1의 총 기여도: 0.18X2의 총 기여도: 0.12X3의 총 기여도: 0.056
전체 특성 중요도의 합:
$$0.18 + 0.12 + 0.056 = 0.356$$
정규화:
X1: $ \frac{0.18}{0.356} \approx 0.51 $X2: $ \frac{0.12}{0.356} \approx 0.34 $X3: $ \frac{0.056}{0.356} \approx 0.16 $
최종 결과
X1의 중요도: 0.51X2의 중요도: 0.34X3의 중요도: 0.16
요약
- Feature Importance는
각 node(노드)에서 purity increament(순수도 향상도)를 누적하고,
이 값의 tree 전체에 대한 기여도를 계산하기 위해 정규화함. - Depth가 증가하면서 추가된 node들 역시 feature importance에 영향을 미치지만,
일반적으로 상위 node에서의 분할이 더 많은 instances에 영향을 주기 때문에 상대적으로 더 높은 중요도를 가질 가능성이 큼.
같이 보면 좋은 자료들
2026.03.24 - [Programming/ML] - XAI: Coefficient, Feature importance, and SHAP
XAI: Coefficient, Feature importance, and SHAP
XAI는 eXplainable AI의 약어로,AI 모델이 왜 이같은 예측(결과)을 내어놓았는지를 설명하는 기술을 가리킴. 이 글은 XAI에서 사용되는 도구들인coefficient,feature importanceSHAP를 비교 설명함.사실 SHAP를 설
dsaint31.tistory.com
Decision Tree - CART (Classification and Regression Trees)
Classification And Regression Trees (CART) 는 Classification와 regression 문제 모두 처리 가능한 Binary Decision Tree를 훈련시키는 알고리즘.1. 역사적 배경1984년 Leo Breiman, Jerome H. Friedman, Richard A. Olshen, Charles J. Stone
ds31x.tistory.com
2023.02.20 - [Computer/CE] - [CE] Tree vs. Graph
[CE] Tree vs. Graph
Graph vs. Tree Graph node와 node를 연결하는 edge로 구성된 data structure(자료구조). object들의 network를 모델링하는데 주로 사용됨. Deep Learning을 가능하게 한 Back-propagation은 Reverse-mode Auto Differentiation에 기반
dsaint31.tistory.com
2024.11.25 - [Programming/DIP] - [DIP] K-Means Tree: Nearest Neighbor Search
[DIP] K-Means Tree: Nearest Neighbor Search
https://gist.github.com/dsaint31x/f7f59e6b99f6ef3dc46e4e7da8826cd9 dip_kmeans_tree.ipynbdip_kmeans_tree.ipynb. GitHub Gist: instantly share code, notes, and snippets.gist.github.com 0. 소개K-Means Tree는대규모 데이터에서효율적으로 Nearest
dsaint31.tistory.com
'Programming > ML' 카테고리의 다른 글
| [ML] Tensor: Scalar, Vector, Matrix. (0) | 2025.02.03 |
|---|---|
| [ML] Nearest Neighbor Search: k-d Tree (0) | 2024.11.25 |
| [ML] Regularization (0) | 2024.10.27 |
| [ML] Linear Classification Model: Hyperplane and Decision Boundary (0) | 2024.10.27 |
| [ML] Diabetes Dataset (0) | 2024.10.05 |