> ## Documentation Index
> Fetch the complete documentation index at: https://wb-21fd5541-update-regex-mention.mintlify.site/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" />

このページでは、XGBoost と W\&B のインテグレーションを使用して、勾配ブースティングのメトリクス、モデルの設定、特徴量重要度、トレーニング済みの booster を自動的にログし、XGBoost の Experiments をトラッキング、比較、再現する方法を紹介します。

W\&B Python SDK (`wandb`) には、XGBoost でのトレーニング中にメトリクス、設定、保存済みの booster をログするための `WandbCallback` コールバックがあります。ここでは、XGBoost の `WandbCallback` による出力を確認できる [ライブの W\&B ダッシュボード](https://wandb.ai/morg/credit_scorecard) を参照できます。

<Frame>
  <img src="https://mintcdn.com/wb-21fd5541-update-regex-mention/mjNowGREZ-o4wwBW/images/integrations/xgb_dashboard.png?fit=max&auto=format&n=mjNowGREZ-o4wwBW&q=85&s=4870610fa1aeed557ec98c19be8cff20" alt="XGBoost を使用した W&B ダッシュボード" width="2756" height="1430" data-path="images/integrations/xgb_dashboard.png" />
</Frame>

<div id="get-started">
  ## はじめに
</div>

W\&B に XGBoost のメトリクス、設定、booster モデルをログするには、`WandbCallback` を XGBoost に渡します。

```python theme={null}
import wandb
from wandb.integration.xgboost import WandbCallback
from xgboost import 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 でのログ記録の詳細については、[XGBoost と W\&B のログ記録ノートブック](https://wandb.me/xgboost)をご覧ください。

<div id="wandbcallback-reference">
  ## `WandbCallback` リファレンス
</div>

<div id="functionality">
  ### 機能
</div>

`WandbCallback` を XGBoost モデルに渡すと、次の処理が行われます。

* Booster モデルの設定を W\&B にログします。
* `rmse` や accuracy など、XGBoost が収集した評価メトリクスを W\&B にログします。
* XGBoost が収集したトレーニング メトリクスを W\&B にログします (`eval_set` にデータを指定した場合) 。
* 最良のスコアと最良のイテレーションをログします。
* トレーニング済みモデルを W\&B Artifacts に保存してアップロードします (`log_model = True` の場合) 。
* `log_feature_importance=True` の場合、特徴量重要度プロットをログします (デフォルト) 。
* `define_metric=True` の場合、最良の eval メトリクスを `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 の場合 (デフォルト) 、`run.summary` には、トレーニングの最後のステップではなく最良のステップでのモデル性能が取得されます。

[WandbCallback のソースコード](https://github.com/wandb/wandb/blob/main/wandb/integration/xgboost/xgboost.py)を確認してください。

その他の例については、[GitHub の examples リポジトリ](https://github.com/wandb/examples/tree/master/examples/boosting-algorithms)を参照してください。

<div id="tune-your-hyperparameters-with-sweeps">
  ## Sweeps でハイパーパラメーターを調整する
</div>

W\&B [Sweeps](/ja/models/sweeps/) は、ハイパーパラメーター探索の experiment を設定、実行、分析するためのツールキットです。このセクションでは、XGBoost インテグレーションと W\&B 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-update-regex-mention/mjNowGREZ-o4wwBW/images/integrations/xgboost_sweeps_example.png?fit=max&auto=format&n=mjNowGREZ-o4wwBW&q=85&s=b72ab90f420f729411d1dfefe19a61bd" alt="XGBoost のパフォーマンス比較" width="1190" height="868" data-path="images/integrations/xgboost_sweeps_example.png" />
</Frame>
