{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Use scikit-learn regressor interface with GPU histogram tree method\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import dask\nfrom dask import array as da\nfrom dask.distributed import Client\n\n# It's recommended to use dask_cuda for GPU assignment\nfrom dask_cuda import LocalCUDACluster\n\nfrom xgboost import dask as dxgb\n\n\ndef main(client: Client) -> dxgb.Booster:\n    # Generate some random data for demonstration\n    rng = da.random.default_rng(1)\n\n    m = 2**18\n    n = 100\n    X = rng.uniform(size=(m, n), chunks=(128**2, -1))\n    y = X.sum(axis=1)\n\n    regressor = dxgb.DaskXGBRegressor(verbosity=1)\n    # Set the device to CUDA\n    regressor.set_params(tree_method=\"hist\", device=\"cuda\")\n    # Assigning client here is optional\n    regressor.client = client\n\n    regressor.fit(X, y, eval_set=[(X, y)])\n    prediction = regressor.predict(X)\n\n    bst = regressor.get_booster()\n    history = regressor.evals_result()\n\n    print(\"Evaluation history:\", history)\n    # returned prediction is always a dask array.\n    assert isinstance(prediction, da.Array)\n    return bst  # returning the trained model\n\n\nif __name__ == \"__main__\":\n    # With dask cuda, one can scale up XGBoost to arbitrary GPU clusters.\n    # `LocalCUDACluster` used here is only for demonstration purpose.\n    with LocalCUDACluster() as cluster:\n        # Create client from cluster, set the backend to GPU array (cupy).\n        with Client(cluster) as client, dask.config.set({\"array.backend\": \"cupy\"}):\n            main(client)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.17"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}