모델 복잡도와 과적합

Python으로 설계하는 Machine Learning 워크플로

Dr. Chris Anagnostopoulos

Honorary Associate Professor

모델 복잡도란?

RandomForestClassifier()max_depth 같은 추가 인자를 받습니다:

help(RandomForestClassifier)
Help on class RandomForestClassifier in module sklearn.ensemble.forest:
...
 |  max_depth : integer or None, optional (default=None)
 |      The maximum depth of the tree. If None, then nodes are expanded until
 |      all leaves are pure or until all leaves contain less than
 |      min_samples_split samples.
Python으로 설계하는 Machine Learning 워크플로
m2 = RandomForestClassifier(
    max_depth=2)
m2.fit(X_train, y_train)

m2.estimators_[0]

깊이 2의 결정나무.

m4 = RandomForestClassifier(
    max_depth=4)
m4.fit(X_train, y_train)

m4.estimators_[0]

깊이 4의 결정나무.

Python으로 설계하는 Machine Learning 워크플로

표준 절차는 데이터를 학습, 테스트(또는 개발), 검증(또는 홀드아웃)으로 분할합니다.

Python으로 설계하는 Machine Learning 워크플로

교차 검증에서는 학습-테스트 분할을 여러 번 수행합니다. 데이터셋을 N개로 나누고, 매번 다른 N-1개로 학습하고 남은 1개로 테스트합니다.

Python으로 설계하는 Machine Learning 워크플로

교차 검증

cross_val_score()로 정확도 평가:

from sklearn.model_selection import cross_val_score

cross_val_score(RandomForestClassifier(), X, y)
array([0.7218 , 0.7682, 0.7866])
numpy.mean(cross_val_score(RandomForestClassifier(), X, y))
0.7589
Python으로 설계하는 Machine Learning 워크플로

모델 복잡도 튜닝

GridSearchCV()로 트리 깊이 튜닝:

from sklearn.model_selection import GridSearchCV
param_grid = {'max_depth':[5,10,20]}
grid = GridSearchCV(RandomForestClassifier(), param_grid)
grid.fit(X,y)
grid._best_params
{'max_depth': 10}
Python으로 설계하는 Machine Learning 워크플로

최대 깊이 3에서 내부 정확도는 0.7에서 시작해, 깊이가 5~30으로 늘면서 거의 1.0까지 증가합니다.

Python으로 설계하는 Machine Learning 워크플로

외부 정확도도 0.7에서 시작해, 깊이 10에서 0.75로 최대가 된 뒤 더 큰 깊이에서는 다시 0.7로 감소합니다.

Python으로 설계하는 Machine Learning 워크플로

깊이 10 이후 구간이 빨간색으로 표시되어 과적합을 나타냅니다.

Python으로 설계하는 Machine Learning 워크플로

복잡하다고 항상 더 좋진 않습니다!

Python으로 설계하는 Machine Learning 워크플로

Preparing Video For Download...