-
4. Boosting Methods (ft. XgBoost)ML 2021. 6. 6. 15:01
안녕하세요. 오늘은 부스팅 기법 중 하나인 XGBoost의 개념과 활용 방법을 알아보겠습니다.
1. 개념
지난 포스팅에서 알아본 Bagging 방안 중 하나인 Random Forest 모델 이외에 Ensembling의 다른 방법 중 하나인 boosting에 대해 알아보도록 하겠습니다. Bagging에서는 투표에 의해서 결과를 정하는 방식이었다면, Boosting이란 weak learner을 strong learner로 변환하여 판단 기준을 정하는 방식입니다. Boosting에서는 각각의 트리들이 최초의 데이터 셋을 편집하여 보유하게 됩니다.
2. 구성
부스터 먹은것과 같이 빨라지는 학습시간(!?) - AdaBoost
Adaboost는 가장 기초적인 boosting 방법입니다. 첫 번째 트리 생성 시에는 모든 관측치에 대하여 같은 가중치를 부여하게 됩니다. 생성된 트리를 검증한 뒤에 판단하기 쉬운 지점에는 가중치를 적게, 판단하기 어려운 지점에는 가중치를 높게 부여하게 됩니다. 변경된 가중치를 사용하여 이후의 트리가 생성이 됩니다. 즉 이미 생성된 모델의 예측치를 통하여 추후 개선하는 방향으로 모델 생성이 진행됩니다. 이러한 과정이 입력된 횟수만큼 반복이 되어 트리를 생성하여 합치게 됩니다. 최종적인 모델은 이전에 생성된 모델 전체 예측치들의 가중합이 됩니다.
- Adaboost와 GBM 차이
두 모델은 boosting을 이용한다는 점에서는 같지만 가장 큰 차이점은 weak learner의 판단 기준이 약한 부분을 인식하는 방법입니다. Adaboost 모델은 가중치가 큰 데이터 지점들을 활용하지만, GBM은 loss function의 gradient를 통하여 판단합니다. Loss function은 모델의 coefficient가 얼마나 데이터를 잘 나타내는지 측정해주는 함수입니다. Loss function은 우리가 무엇을 "최적화" 할 것이냐에 따라 그 논리가 달라지게 됩니다. 예를 들어 상품 가격을 회귀를 활용하여 예측하고자 한다면 사용해야 할 loss function은 실제 상품 가격과 예측된 상품 가격의 차이일 것입니다(ex: mse). 만약, 분류를 통한 대출 판단을 하고자 한다면 사용될 loss function은 현재 분류 모델이 얼마나 대출을 안 해줘야 하는 고객을 잘 분류하는지가 될 것입니다(ex: recall). 그리고 이미 알려져 있는 loss function 이외에 추가로 cunstom loss function을 생성하여서 적용할 수도 있다는 것도 GBM을 사용하는 이유라고 할 수 있겠습니다.
- XGBoost 구성요소
XGBoost는 Decision Tree를 사용한 boosting ensemble model입니다. 방대한 비정형 데이터(자연어, 이미지 등)에서는 딥러닝 방식이 다른 알고리즘보다 높은 성능을 보이지만 규모가 상대적으로 적은 정형 데이터(tabular data)에서는 decision tree를 기반으로한 XGBoost나 lightgbm이 더 높은 성능을 보이고 있습니다.
XGBoost 구성요소 Source: https://towardsdatascience.com/https-medium-com-vishalmorde-xgboost-algorithm-long-she-may-rein-edd9f99be63d 시스템 특징
1. 병렬화
- 트리 모델들은 base learner생성시 리프 노드를 도는 outer loop와 내부 특징을 계산하는 inner loop로 나뉘게 됩니다. 병렬화를 가능하게 하려면 이 두 개의 루프가 동시에 돌아가야 합니다. XGBoost는 모든 instance를 global scan 하여 초기화하고 병렬 thread로 정렬하는 방식으로 루프의 순서를 변경하여 런타임을 향상합니다.
2. Pruning
- GBM 프레임워크는 트리 분할 시 negative loss criterion을 사용하기에 greedy algorithm이라고도 합니다. XGBoost는 max_depth 파라미터를 사용해서 먼저 역으로 pruning을 진행합니다. 이와 같은 depth-first 방식은 계산적 퍼포먼스를 크게 증가시킵니다.
3. 하드웨어 최적화
- 캐시 확인과 internal buffer를 thread에 재분배하여 gradient statistics를 저장함으로 하드웨어를 효율적으로 사용합니다. 메모리 사이즈에 맞지 않을 정도로 큰 data frame들을 다루는 것도 가능합니다.
알고리즘 특징
1. Regularizaiton
- LASSO(L2)과 Ridge(L2) regularization을 사용하여 overfitting을 방지합니다.
2. Sparsity Awareness
- Sparse 한 변수들을 허용합니다. training loss에 기반하여 최적의 빈칸을 학습하고 또한 여러 sparsity 패턴을 다룹니다.
3. Weighted Quantile Sketch
- Weighted Quantile Sketch 알고리즘을 활용하여 가중치가 부여된 데이터에서 최적의 분할 지점을 찾습니다.
4. Cross Validation
- 회차가 진행 중일 때 built in cross validation이 진행됩니다. 별도의 cross validation의 필요성을 줄여줍니다.
3. 활용방안
이번에는 공식 xgboost document의 python 코드 예시로 활용 방법을 알아보겠습니다.
코드 scorce: https://github.com/dmlc/xgboost/blob/master/demo/guide-python/sklearn_examples.py
dmlc/xgboost
Scalable, Portable and Distributed Gradient Boosting (GBDT, GBRT or GBM) Library, for Python, R, Java, Scala, C++ and more. Runs on single machine, Hadoop, Spark, Dask, Flink and DataFlow - dmlc/x...
github.com
import pickle import xgboost as xgb import numpy as np from sklearn.model_selection import KFold, train_test_split, GridSearchCV from sklearn.metrics import confusion_matrix, mean_squared_error from sklearn.datasets import load_iris, load_digits, load_boston rng = np.random.RandomState(31337) print("Zeros and Ones from the Digits dataset: binary classification") digits = load_digits(n_class=2) y = digits['target'] X = digits['data'] kf = KFold(n_splits=2, shuffle=True, random_state=rng) # K fold cross validaiton for train_index, test_index in kf.split(X): xgb_model = xgb.XGBClassifier(n_jobs=1).fit(X[train_index], y[train_index]) # 분류 모델 predictions = xgb_model.predict(X[test_index]) actuals = y[test_index] print(confusion_matrix(actuals, predictions)) print("Iris: multiclass classification") iris = load_iris() y = iris['target'] X = iris['data'] kf = KFold(n_splits=2, shuffle=True, random_state=rng) for train_index, test_index in kf.split(X): xgb_model = xgb.XGBClassifier(n_jobs=1).fit(X[train_index], y[train_index]) # 분류 모델 predictions = xgb_model.predict(X[test_index]) actuals = y[test_index] print(confusion_matrix(actuals, predictions)) print("Boston Housing: regression") boston = load_boston() y = boston['target'] X = boston['data'] kf = KFold(n_splits=2, shuffle=True, random_state=rng) for train_index, test_index in kf.split(X): xgb_model = xgb.XGBRegressor(n_jobs=1).fit(X[train_index], y[train_index]) # 회귀 모델 predictions = xgb_model.predict(X[test_index]) actuals = y[test_index] print(mean_squared_error(actuals, predictions)) print("Parameter optimization") y = boston['target'] X = boston['data'] xgb_model = xgb.XGBRegressor(n_jobs=1) clf = GridSearchCV(xgb_model, {'max_depth': [2, 4, 6], 'n_estimators': [50, 100, 200]}, verbose=1, n_jobs=1) # Grid Search clf.fit(X, y) print(clf.best_score_) print(clf.best_params_) # The sklearn API models are picklable print("Pickling sklearn API models") # must open in binary format to pickle pickle.dump(clf, open("best_boston.pkl", "wb")) clf2 = pickle.load(open("best_boston.pkl", "rb")) print(np.allclose(clf.predict(X), clf2.predict(X))) # Early-stopping X = digits['data'] y = digits['target'] X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0) clf = xgb.XGBClassifier(n_jobs=1) # 분류 모델 clf.fit(X_train, y_train, early_stopping_rounds=10, eval_metric="auc", eval_set=[(X_test, y_test)])
참고자료
XGBoost Algorithm: Long May She Reign!
The new queen of Machine Learning algorithms taking over the world…
towardsdatascience.com
XGBoost Documentation — xgboost 1.5.0-dev documentation
xgboost.readthedocs.io
Understanding Gradient Boosting Machines
Motivation:
towardsdatascience.com
'ML' 카테고리의 다른 글
3. Random Forest (0) 2021.05.09 2. Tree - Based Model (Decision Tree🌳) (0) 2021.04.18 1. 지표.zip (Classifier Model Evaluation Metrics) (0) 2021.03.28