분류 트리 학습

Python으로 배우는 트리 기반 Machine Learning

Elie Kawerk

Data Scientist

의사결정나무의 구성 요소

  • 의사결정나무: 노드의 계층 구조로 된 데이터 구조.

  • 노드: 질문 또는 예측.

Python으로 배우는 트리 기반 Machine Learning

의사결정나무의 구성 요소

세 종류의 노드:

  • 루트: 상위 노드 없음, 질문 → 자식 노드 2개.

  • 내부 노드: 상위 노드 1개, 질문 → 자식 노드 2개.

  • 리프: 상위 노드 1개, 자식 노드 없음 → 예측.

Python으로 배우는 트리 기반 Machine Learning

예측

DT-labeled

Python으로 배우는 트리 기반 Machine Learning

정보 이득 (IG)

IG-diagram

Python으로 배우는 트리 기반 Machine Learning

정보 이득 (IG)

IG-formula

노드의 불순도를 측정하는 기준 $I (node)$:

  • 지니 지수,
  • 엔트로피. ...
Python으로 배우는 트리 기반 Machine Learning

분류 트리 학습

  • 노드는 재귀적으로 성장합니다.

  • 각 노드에서 다음을 기준으로 데이터를 분할합니다:

    • 특성 $f$와 분할점 $sp$를 선택하여 $IG(\text{node})$를 최대화.
  • $IG (\text{node})$ = 0이면 해당 노드를 리프로 선언.

    ...

Python으로 배우는 트리 기반 Machine Learning
# Import DecisionTreeClassifier
from sklearn.tree import DecisionTreeClassifier
# Import train_test_split
from sklearn.model_selection import train_test_split
# Import accuracy_score
from sklearn.metrics import accuracy_score
# Split dataset into 80% train, 20% test
X_train, X_test, y_train, y_test= train_test_split(X, y, 
                                                   test_size=0.2, 
                                                   stratify=y,
                                                   random_state=1)
# Instantiate dt, set 'criterion' to 'gini'
dt = DecisionTreeClassifier(criterion='gini', random_state=1)
Python으로 배우는 트리 기반 Machine Learning

scikit-learn의 정보 기준

# Fit dt to the training set
dt.fit(X_train,y_train)

# Predict test-set labels
y_pred= dt.predict(X_test)

# Evaluate test-set accuracy
accuracy_score(y_test, y_pred)
0.92105263157894735
Python으로 배우는 트리 기반 Machine Learning

실습해 봅시다!

Python으로 배우는 트리 기반 Machine Learning

Preparing Video For Download...