10.결정트리와 앙상블(AdaBoost)
글 한눈에 보기
문제 설정
10.결정트리와 앙상블(AdaBoost)를 중심으로 학습한 내용을 정리한 ML 실습입니다.
원본 구조
원본 마크다운의 큰 섹션 흐름을 기준으로 이 실습을 다시 읽을 수 있게 정리했습니다.
데이터 맥락
원본 노트에서 데이터를 설명한 부분을 기준으로 실습 맥락을 정리했습니다.
주요 장
전처리와 입력 정리 · 분류 문제 · 결정 트리와 앙상블 · 피처 엔지니어링
구현 흐름
학습/검증 데이터 분리 -> DecisionTree 모델 학습 -> 분류 성능 평가
자료
ipynb / md · 코드 8 · 실행 7
주요 스택
sklearn, matplotlib, numpy
from sklearn.ensemble import AdaBoostClassifier
from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import ListedColormap # 결정 경계 시각화 하기 위한 라이브러리
X, y = make_moons(n_samples=1000, noise=0.3, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
base_estimator = DecisionTreeClassifier(max_depth=1) # Stump생성
ada_clf = AdaBoostClassifier(
estimator=base_estimator,
n_estimators=1000,
learning_rate=0.1
)
ada_clf.fit(X_train, y_train)
y_pred = ada_clf.predict(X_test)
acc = accuracy_score(y_test, y_pred)
acc
def plot_decision_boundary(model, X, y, title="Decision Boundary"):
cmap_light = ListedColormap(['#FFAAAA', '#AAAAFF'])
cmap_bold = ListedColormap(['#FF0000', '#0000FF'])
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
np.arange(y_min, y_max, 0.02))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(8, 6))
plt.contourf(xx, yy, Z, cmap=cmap_light)
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold, edgecolor='k', s=30)
plt.title(title)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.grid(True)
plt.show()
plot_decision_boundary(ada_clf, X, y, title="AdaBoost Decision Boundary")