> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wandb.ai/llms.txt
> Use this file to discover all available pages before exploring further.

> W&B를 XGBoost와 통합해 그래디언트 부스팅 메트릭, 특성 중요도, 모델 성능을 자동으로 로깅합니다.

# XGBoost

export const ColabLink = ({url}) => <a href={url} target="_blank" rel="noopener noreferrer" className="colab-link">
    <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
      <path d="M14.25.18l.9.2.73.26.59.3.45.32.34.34.25.34.16.33.1.3.04.26.02.2-.01.13V8.5l-.05.63-.13.55-.21.46-.26.38-.3.31-.33.25-.35.19-.35.14-.33.1-.3.07-.26.04-.21.02H8.77l-.69.05-.59.14-.5.22-.41.27-.33.32-.27.35-.2.36-.15.37-.1.35-.07.32-.04.27-.02.21v3.06H3.17l-.21-.03-.28-.07-.32-.12-.35-.18-.36-.26-.36-.36-.35-.46-.32-.59-.28-.73-.21-.88-.14-1.05-.05-1.23.06-1.22.16-1.04.24-.87.32-.71.36-.57.4-.44.42-.33.42-.24.4-.16.36-.1.32-.05.24-.01h.16l.06.01h8.16v-.83H6.18l-.01-2.75-.02-.37.05-.34.11-.31.17-.28.25-.26.31-.23.38-.2.44-.18.51-.15.58-.12.64-.1.71-.06.77-.04.84-.02 1.27.05zm-6.3 1.98l-.23.33-.08.41.08.41.23.34.33.22.41.09.41-.09.33-.22.23-.34.08-.41-.08-.41-.23-.33-.33-.22-.41-.09-.41.09zm13.09 3.95l.28.06.32.12.35.18.36.27.36.35.35.47.32.59.28.73.21.88.14 1.04.05 1.23-.06 1.23-.16 1.04-.24.86-.32.71-.36.57-.4.45-.42.33-.42.24-.4.16-.36.09-.32.05-.24.02-.16-.01h-8.22v.82h5.84l.01 2.76.02.36-.05.34-.11.31-.17.29-.25.25-.31.24-.38.2-.44.17-.51.15-.58.13-.64.09-.71.07-.77.04-.84.01-1.27-.04-1.07-.14-.9-.2-.73-.25-.59-.3-.45-.33-.34-.34-.25-.34-.16-.33-.1-.3-.04-.25-.02-.2.01-.13v-5.34l.05-.64.13-.54.21-.46.26-.38.3-.32.33-.24.35-.2.35-.14.33-.1.3-.06.26-.04.21-.02.13-.01h5.84l.69-.05.59-.14.5-.21.41-.28.33-.32.27-.35.2-.36.15-.36.1-.35.07-.32.04-.28.02-.21V6.07h2.09l.14.01.21.03zm-6.47 14.25l-.23.33-.08.41.08.41.23.33.33.23.41.08.41-.08.33-.23.23-.33.08-.41-.08-.41-.23-.33-.33-.23-.41-.08-.41.08z" />
    </svg>
    Colab에서 사용해 보기
  </a>;

<ColabLink url="https://colab.research.google.com/github/wandb/examples/blob/master/colabs/boosting/Credit_Scorecards_with_XGBoost_and_W%26B.ipynb" />

`wandb` 라이브러리에는 XGBoost로 트레이닝할 때 메트릭, 설정, 저장된 부스터를 로깅하는 `WandbCallback` 콜백이 있습니다. 여기에서 XGBoost `WandbCallback`의 출력이 표시된 [실시간 W\&B 대시보드](https://wandb.ai/morg/credit_scorecard)를 확인할 수 있습니다.

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541/w-lBKSCruauC3-2f/images/integrations/xgb_dashboard.png?fit=max&auto=format&n=w-lBKSCruauC3-2f&q=85&s=5a8652cd442eeb41733d541a934f733b" alt="XGBoost를 사용한 W&B 대시보드" width="2756" height="1430" data-path="images/integrations/xgb_dashboard.png" />
</Frame>

<div id="get-started">
  ## 시작하기
</div>

XGBoost 메트릭, 설정, 부스터 모델은 `WandbCallback`을 XGBoost에 전달하기만 하면 W\&B에 손쉽게 로깅할 수 있습니다:

```python theme={null}
from wandb.integration.xgboost import WandbCallback
import xgboost as XGBClassifier

...
# wandb run 시작
with wandb.init() as run:
  # 모델에 WandbCallback 전달
  bst = XGBClassifier()
  bst.fit(X_train, y_train, callbacks=[WandbCallback(log_model=True)])
```

XGBoost와 W\&B에서의 로깅을 자세히 살펴보려면 [이 노트북](https://wandb.me/xgboost)을 열어보세요.

<div id="wandbcallback-reference">
  ## `WandbCallback` 레퍼런스
</div>

<div id="functionality">
  ### 기능
</div>

`WandbCallback`을 XGBoost 모델에 전달하면 다음 작업이 수행됩니다.

* 부스터 모델 설정을 W\&B에 로깅합니다
* rmse, accuracy 등 XGBoost가 수집한 평가 메트릭을 W\&B에 로깅합니다
* XGBoost가 수집한 트레이닝 메트릭을 로깅합니다 (`eval_set`에 데이터를 제공한 경우)
* 최고 score와 최적 iteration을 로깅합니다
* 트레이닝된 모델을 W\&B Artifacts에 저장하고 업로드합니다 (`log_model = True`인 경우)
* `log_feature_importance=True`일 때 특성 중요도 플롯을 로깅합니다(기본값).
* `define_metric=True`일 때 최상의 eval metric을 `wandb.Run.summary`에 저장합니다(기본값).

<div id="arguments">
  ### 인수
</div>

* `log_model`: (boolean) True이면 모델을 W\&B Artifacts에 저장하고 업로드합니다.

* `log_feature_importance`: (boolean) True이면 특성 중요도 막대 그래프를 기록합니다.

* `importance_type`: (str) 트리 모델의 경우 `{weight, gain, cover, total_gain, total_cover}` 중 하나입니다. 선형 모델의 경우 `weight`입니다.

* `define_metric`: (boolean) True이면(기본값) 트레이닝의 마지막 step이 아니라 최적 step에서의 모델 performance를 `run.summary`에 기록합니다.

[WandbCallback의 소스 코드](https://github.com/wandb/wandb/blob/main/wandb/integration/xgboost/xgboost.py)를 검토할 수 있습니다.

추가 예시는 [GitHub의 예제 저장소](https://github.com/wandb/examples/tree/master/examples/boosting-algorithms)에서 확인하세요.

<div id="tune-your-hyperparameters-with-sweeps">
  ## Sweeps로 하이퍼파라미터 조정하기
</div>

모델의 성능을 최대한 끌어올리려면 트리 깊이와 학습률 같은 하이퍼파라미터를 조정해야 합니다. W\&B [Sweeps](/ko/models/sweeps/)는 대규모 하이퍼파라미터 테스트 실험을 설정하고, 조율하고, 분석할 수 있는 강력한 도구입니다.

<ColabLink url="https://colab.research.google.com/github/wandb/examples/blob/master/colabs/boosting/Using_W%26B_Sweeps_with_XGBoost.ipynb" />

이 [XGBoost & Sweeps Python 스크립트](https://github.com/wandb/examples/blob/master/examples/wandb-sweeps/sweeps-xgboost/xgboost_tune.py)도 사용해 보세요.

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541/w-lBKSCruauC3-2f/images/integrations/xgboost_sweeps_example.png?fit=max&auto=format&n=w-lBKSCruauC3-2f&q=85&s=70f40737f6467eaab407e3b3a42c48d9" alt="XGBoost 성능 비교" width="1190" height="868" data-path="images/integrations/xgboost_sweeps_example.png" />
</Frame>
