편향과 분산 문제 진단

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

Elie Kawerk

Data Scientist

일반화 오차 추정

  • 모델의 일반화 오차를 어떻게 추정할까요?

  • 직접 추정할 수 없는 이유:

    • $f$를 알 수 없고,

    • 보통 데이터셋이 하나뿐이며,

    • 노이즈는 예측 불가능합니다.

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

일반화 오차 추정

해결 방법:

  • 데이터를 훈련 세트와 테스트 세트로 분할,
  • $\hat{f}$를 훈련 세트에 적합,
  • 미관측 테스트 세트에서 $\hat{f}$의 오차 평가.
  • $\hat{f}$의 일반화 오차 $\approx$ $\hat{f}$의 테스트 세트 오차.
Python으로 배우는 트리 기반 Machine Learning

교차 검증으로 더 나은 모델 평가

  • $\hat{f}$의 성능에 확신이 생기기 전까지 테스트 세트는 사용하지 않아야 합니다.

  • 훈련 세트로 $\hat{f}$ 평가: 편향된 추정값, $\hat{f}$이 이미 모든 훈련 데이터를 학습했기 때문입니다.

  • 해결책 $\rightarrow$ 교차 검증(CV):

    • K-겹 CV,

    • 홀드아웃 CV.

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

K-겹 CV

K-겹 CV

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

K-겹 CV

CV 오차

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

분산 문제 진단

  • $\hat{f}$가 높은 분산 문제를 가질 때:

    $\hat{f}$의 CV 오차 > $\hat{f}$의 훈련 세트 오차.

  • $\hat{f}$가 훈련 세트에 과적합되었다고 합니다. 과적합 해결 방법:
    • 모델 복잡도 줄이기,
    • 예: 최대 깊이 감소, 리프당 최소 샘플 수 증가, ...
    • 더 많은 데이터 수집, ..
Python으로 배우는 트리 기반 Machine Learning

편향 문제 진단

  • $\hat{f}$가 높은 편향 문제를 가질 때:

    $\hat{f}$의 CV 오차 $\approx$ $\hat{f}$의 훈련 세트 오차 $>>$ 목표 오차.

  • $\hat{f}$가 훈련 세트에 과소적합되었다고 합니다. 과소적합 해결 방법:

    • 모델 복잡도 높이기
    • 예: 최대 깊이 증가, 리프당 최소 샘플 수 감소, ...
    • 더 관련성 높은 특성 수집
Python으로 배우는 트리 기반 Machine Learning

Auto 데이터셋에서 sklearn K-겹 CV

from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error as MSE
from sklearn.model_selection import cross_val_score

# Set seed for reproducibility SEED = 123 # Split data into 70% train and 30% test X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.3, random_state=SEED)
# Instantiate decision tree regressor and assign it to 'dt' dt = DecisionTreeRegressor(max_depth=4, min_samples_leaf=0.14, random_state=SEED)
Python으로 배우는 트리 기반 Machine Learning

Auto 데이터셋에서 sklearn K-겹 CV

# Evaluate the list of MSE ontained by 10-fold CV 
# Set n_jobs to -1 in order to exploit all CPU cores in computation
MSE_CV = - cross_val_score(dt, X_train, y_train, cv= 10, 
                           scoring='neg_mean_squared_error',
                           n_jobs = -1)

# Fit 'dt' to the training set dt.fit(X_train, y_train) # Predict the labels of training set y_predict_train = dt.predict(X_train) # Predict the labels of test set y_predict_test = dt.predict(X_test)
Python으로 배우는 트리 기반 Machine Learning
# CV MSE  
print('CV MSE: {:.2f}'.format(MSE_CV.mean()))
CV MSE: 20.51
# Training set MSE
print('Train MSE: {:.2f}'.format(MSE(y_train, y_predict_train)))
Train MSE: 15.30
# Test set MSE
print('Test MSE: {:.2f}'.format(MSE(y_test, y_predict_test)))
Test MSE: 20.92
Python으로 배우는 트리 기반 Machine Learning

연습해 봅시다!

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

Preparing Video For Download...