data-science-summary/summary

GradientBoost in python

고수트 2020. 9. 20. 17:10
반응형

<엔지니어는 구현을 못하면 모르는것이다>

GradientBoost Algorithm

Boosting 방식의 일종

학습된 분류기의 약점을 알려주고 이 약점을 중점으로 보완을 하는방식 , 즉 점진적 개선이라 Gradient

Leaf Node 하나로 아주 간단한 모델(A)로 시작 => 이 모델(A)로 예측하고 남은 잔차(Residual) 계산

=> 그리고 이 잔차를 예측하는 모델(B)을 만들고 기존 모델(A)와 결합하여 정답 예측한다면 기존 모델(A)보다 나은 모델(B)생성가능

=> 이 방법을 반복하면서 점차 나은 예측 모델을 만드는것이 GMB

=> 단점 : 과적합 => learning rate, regularization 을 통해 해결가능

from sklearn.datasets import make_classification
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split

X, y = make_classification(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
# learning_rate : 학습률 / n_estimators: 결정 트리개수 default 10, max_depth: 트리 깊이
clf = GradientBoostingClassifier(learning_rate=0.05, n_estimators= 12, max_depth=10, random_state=0)
clf.fit(X_train, y_train)
print(clf.predict(X_test))

# 정확도
print(clf.score(X_test, y_test))

# feature의 중요도 파악 
clf.feature_importances_
반응형