reference
XGBRegressor 하이퍼파라미터 예
XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=1, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.1, max_delta_step=0, max_depth=5,
min_child_weight=1, missing=nan, monotone_constraints='()',
n_estimators=100, n_jobs=0, num_parallel_tree=1, random_state=0,
reg_alpha=0, reg_lambda=1, scale_pos_weight=1, subsample=1,
tree_method='exact', validate_parameters=1, verbosity=None)
XGBoost는 다수의 하이퍼파라미터가 존재하며, 다음 세가지 범주로 나뉜다.
일반 파라미터
부스터 파라미터
학습 과정 파라미터
민감하게 조정해야하는 것
과적합 방지를 위해 조정해야하는 것
import xgboost as xgb
import matplotlib.pyplot as plt
# 모델 선언
model = xgb.XGBClassifier()
# 모델 훈련
model.fit(x,y)
# 모델 예측
y_pred = model.predict(X_test)
import xgboost as xgb
# 모델 선언
my_model = xgb.XGBRegressor(learning_rate=0.1,max_depth=5,n_estimators=100)
# 모델 훈련
my_model.fit(X_train, y_train, verbose=False)
# 모델 예측
y_pred = my_model.predict(X_test)
XGBoost 모형을 시각화함으로써 개발한 예측모형의 성능에 대해 더 깊은 이해를 가질 수 있다.
특히 의사결정나무 시각화를 위해서 graphviz 라이브러리 설치가 필요한데, 경로명 설정문제도 함께 해결하는 방식으로 아래와 같이 pip install, conda install 명령어를 연이어 실행시킨다.
pip install graphviz
conda install graphviz
xgb.plot_importance() 메쏘드에 XGBoost 모형객체를 넣어 변수중요도를 파악할 수 있다.
import xgboost as xgb
xgb.plot_importance(my_model)

xgb.plot_tree() 메쏘드에 XGBoost 모형객체를 넣어 의사결정트리를 시각화할 수 있다.
기본값으로 트리를 저장하면 읽을 수 없을 정도로 낮은 해상도의 이미지가 생성된다.
이미지 크기 또는 해상도를 지정하는 옵션을 다음과 같이 지정해야한다.
import xgboost as xgb
import matplotlib.pyplot as plt
# num_trees : 그림을 여러개 그릴시 그림 번호
# rankdir : 트리의 방향, 디폴트는 위아래 방향
# rankdir="LR" : 왼쪽에서 오른쪽 방향으로 트리를 보여준다.
xgb.plot_tree(my_model, num_trees=0, rankdir='LR')
fig = plt.gcf()
fig.set_size_inches(150, 100)
# 이미지 저장하고 싶다면
# fig.savefig('tree.png')
plt.show()

Source : https://wooono.tistory.com/97