여러 개 모델 결합, 개별 모델의 약점 보완
병렬 앙상블
개별 모델 예측 결과를 집계해 최종 예측 결과 결정
다양한 예측 방식 결합가능
하드보팅 : 각 분류 모델의 최종 예측 결과의 다수결로 최종 예측 결정
소프트 보팅 : 각 분류 모델의 예측 확률 값을 평균 내에 가장 높은 확률을 가진 클래스로 최종 예측을 결정
부트스트랩한 데이터로 모델을 학습, 모델 예측결과를 집계
같은 유형의 알고리즘 기반 모델 사용
데이터 분할 시 중복 허용
범주형 데이터는 투표방식
연속형 데이터는 평균으로 결과집계
대표 알고리즘 : random forest
부스트스트랩?
원본 데이터셋에서 중복을 허용해 무작위 샘플링 수행
-> 크기 같은 새로운 데이터셋을 여러 개 만듦
-> 배깅에서 새로운 서로 다른 데이터셋 학습
-> 개별 모델의 과적합 된 부분을 상쇄
-> 결국 더 일반화된 성능 보일 수 있음
회귀모델
# 불러오기
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_absolute_error, r2_score
# 선언하기
model = RandomForestRegressor(max_depth=5, n_estimators=100, random_state=1)
# 학습하기
model.fit(x_train, y_train)
# 예측하기
y_pred = model.predict(x_test)
# 평가하기
print(mean_absolute_error(y_test, y_pred))
print(r2_score(y_test, y_pred)
분류모델
# 불러오기
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix, classification_report
# 선언하기
model = RandomForestClassifier(max_depth=5, n_estimators=100, random_state=1)
# 학습하기
model.fit(x_train, y_train)
# 예측하기
y_pred = model.predict(x_test)
# 평가하기
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))
대표 알고리즘 : XGbBoost, LightGBM
같은 유형 알고리즘 기반 모델 여러 개에 대해 순차적으로 학습 수행
이전 모델이 제대로 예측 못한 데이터에 가중치를 부여해 다음모델이 학습 및 예측 진행
배깅에 비해 성능 GOOD,속도는 느림, 과적합 발생 가능성이 있음.
회귀모델
# 불러오기
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error, r2_score
# 선언하기
model = XGBRegressor(max_depth=5, n_estimators=100, random_state=1)
# 학습하기
model.fit(x_train, y_train)
# 예측하기
y_pred = model.predict(x_test)
# 평가하기
print(mean_absolute_error(y_test, y_pred))
print(r2_score(y_test, y_pred))
분류모델
# 불러오기
from xgboost import XGBClassifier
from sklearn.metrics import confusion_matrix, classification_report
# 선언하기
model = XGBClassifier(max_depth=5, n_estimators=100, random_state=1)
# 학습하기
model.fit(x_train, y_train)
# 예측하기
y_pred = model.predict(x_test)
# 평가하기
print(confusion_matrix(y_test, y_pred))
print(classification_report(y_test, y_pred))