Python으로 배우는 트리 기반 Machine Learning
Elie Kawerk
Data Scientist
모델의 일반화 오차를 어떻게 추정할까요?
직접 추정할 수 없는 이유:
$f$를 알 수 없고,
보통 데이터셋이 하나뿐이며,
노이즈는 예측 불가능합니다.
해결 방법:
$\hat{f}$의 성능에 확신이 생기기 전까지 테스트 세트는 사용하지 않아야 합니다.
훈련 세트로 $\hat{f}$ 평가: 편향된 추정값, $\hat{f}$이 이미 모든 훈련 데이터를 학습했기 때문입니다.
해결책 $\rightarrow$ 교차 검증(CV):
K-겹 CV,
홀드아웃 CV.


$\hat{f}$가 높은 분산 문제를 가질 때:
$\hat{f}$의 CV 오차 > $\hat{f}$의 훈련 세트 오차.
$\hat{f}$가 높은 편향 문제를 가질 때:
$\hat{f}$의 CV 오차 $\approx$ $\hat{f}$의 훈련 세트 오차 $>>$ 목표 오차.
$\hat{f}$가 훈련 세트에 과소적합되었다고 합니다. 과소적합 해결 방법:
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)
# 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)
# 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