XGBoost 및 우승자 인터뷰 소개, 부스팅 알고리즘으로 점수 올리기
XGBoost: A Scalable Tree Boosting System
- 2015년 캐글 블로그에 xgboost를 사용하여 17건의 우승 솔루션이 공유됨 : Dato Winners’ Interview: 1st place, Mad Professors | No Free Hunch
- 2016년 논문이 등록 됨 : XGBoostArxiv.pdf
- 공식문서 : XGBoost Documents
- 분산형 그래디언트 부스팅 알고리즘
- 부스팅 알고리즘은?
- 부스팅 알고리즘은 약한 예측모형들을 결합하여 강한 예측모형을 만드는 알고리즘
- 배깅과 유사하게 초기 샘플데이터로 다수의 분류기를 만들지만 배깅과 다르게 순차적이다.
- 랜덤포레스트의 배깅과는 다르게 이전 트리의 오차를 보완하는 방식으로 순차적으로 트리를 만듦
- 결정트리(Decision Tree) 알고리즘의 연장선에 있음
- 여러 개의 결정트리를 묶어 강력한 모델을 만드는 앙상블 방법
- 분류와 회귀에 사용할 수 있음
- 무작위성이 없으며 강력한 사전 가지치기를 사용
- 참고 이미지 : http://www.birc.co.kr/2017/02/06/%EC%95%99%EC%83%81%EB%B8%94ensemble-%EB%B6%80%EC%8A%A4%ED%8C%85boosting/
- 배깅과 부스팅의 차이점은 udacity에서 설명한 영상이 가장 도움이 되었음
- 타이타닉 경진대회에 사용 예제가 있음 XGBoost example (Python) | Kaggle
Mac OSX에서 XGBoost 설치하기
A Gentle Introduction to XGBoost for Applied Machine Learning - Machine Learning Mastery
https://speakerdeck.com/datasciencela/tianqi-chen-xgboost-overview-and-latest-news-la-meetup-talk
datacamp의 XGboost 온라인 강의 : Extreme Gradient Boosting with XGBoost
import xgboost as xgb
dtrain = xgb.DMatrix(X_train_tfidf_vector, label=train['sentiment'])
# 멀티프로세싱은 nthread 였는데 n_jobs로 변경되었다고 한다.
# 설치 된 xgboost버전에 따라 파라메터가 다를 수 있으니
# 이 코드를 돌리고 터미널에서 $ top -o cpu 로 CPU자원을 100%넘게 사용하고 있는지 확인해 본다.
params = {
'booster': 'gblinear',
'objective': 'multi:softmax',
'eval_metric': 'merror',
'eta' : 0.02,
'lambda': 2.0,
'alpha': 1.0,
'lambda_bias': 6.0,
'num_class': 5,
'n_jobs' : 4,
'silent': 1,
}
%time booster = xgb.train(params, dtrain, num_boost_round=100)
CPU times: user 24.2 s, sys: 1.19 s, total: 25.4 s
Wall time: 18.9 s
dtest = xgb.DMatrix(X_test_tfidf_vector)
result = booster.predict(dtest)
print(result.shape)
result[0:10]
(25000,)
array([1., 0., 1., 1., 1., 1., 0., 0., 0., 1.], dtype=float32)
result = result.astype(int)
output = pd.DataFrame(data={'id':test['id'], 'sentiment':result})
output.head()
id | sentiment | |
---|---|---|
0 | "12311_10" | 1 |
1 | "8348_2" | 0 |
2 | "5828_4" | 1 |
3 | "7186_2" | 1 |
4 | "12128_7" | 1 |
output_sentiment = output['sentiment'].value_counts()
print(output_sentiment[0] - output_sentiment[1])
output_sentiment
-704
1 12852
0 12148
Name: sentiment, dtype: int64
output.to_csv("data/tutorial_4_tfidf_xgboost.csv", index=False, quoting=3)
Kaggle API를 통해 서브미션이 가능하다.
# !kaggle competitions submit -c word2vec-nlp-tutorial -f data/tutorial_4_tfidf_xgboost.csv -m 'num_boost_round=300'
Successfully submitted to Bag of Words Meets Bags of Popcorn
# !kaggle competitions submissions -c word2vec-nlp-tutorial
# 캐글 스코어 0.86560
286/578
0.49480968858131485
-
output_sentiment값 출력 에러가 납니다.
-
2019.5.10 16:04
1